how to get a pc list that have a service running o

2019-09-22 06:55发布

I m using psexec to auto run from cmd on all PCs on our network to check if certain process is running or not. but i wannt a list with all pc name that has the service running on. how can i do it from powershell?

this is what i m running now. 2 batch files and 1 text file.

get.bat


tasklist | findstr pmill.exe >> dc-01\c$\0001.txt


run_get.bat


psexec @%1 -u administrator -p password -c "C:\get.bat"


pclist.txt


what i got from this in result are just all pmill.exe , i m wondering if there is anyway that i can output the PC name that has pmill.exe running on?

Hint plz!

2条回答
我想做一个坏孩纸
2楼-- · 2019-09-22 07:25

If all computers have powershell installed with remoting enabled, you can try the script below. It also outputs computers that were not reachable so you can retest them later if you want to. If you don't need it, just remove the content inside the catch-block(or all of try/catch):

$out = @()
Get-Content "pclist.txt" | foreach {
    $pc = $_ 
    try {
        if((Get-Process -Name "pmill" -ComputerName $pc) -ne $null) {
            $out += $_
        }
    } catch { 
        #Unknown error
        $out += "ERROR: $pc was not checked. $_.Message"
    }
}

$out | Set-Content "out.txt"

pclist.txt:

graimer-pc
pcwithoutprocesscalledpmill
testcomputer
testpc
graimer-pc

Out.txt (log):

graimer-pc
ERROR: testcomputer is unreachable
ERROR: testpc is unreachable
graimer-pc
查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-22 07:39

Depending on what kind of remoting is available:

  • If Windows remote management (eg. Services.msc can connect) then just use

    Get-Service -Name theService -computer TheComputer
    

    which will return an object if the service is running with information on that service (like its status) or nothing if it isn't installed, so assuming pclist.txt is one computer name per line, to get a list of computers where the service is running (after replacing serviceName with the correct name: this is likely to be different to the process name):

    Get-Content pclist.txt | Where-Object {
      $s = Get-Service -name 'serviceName' -computer $_
      $s -or ($s.Status -eq Running)
    }
    
  • If WMI is available using Get-WmiObject win32_service -filter 'name="serviceName"' and theStatemember of the returned object in theWhere-Object` above.

  • PowerShell remoting: use Invoke-Command -ComputerName dev1 -ScriptBlock { Get-Service serviceName } run the Get-Service on the remote machine to return the same object (but with PSComputerName property added)

查看更多
登录 后发表回答