how to get a pc list that have a service running on each pc?

喜夏-厌秋 提交于 2020-01-08 04:19:09

问题


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!


回答1:


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



回答2:


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)



来源:https://stackoverflow.com/questions/14985368/how-to-get-a-pc-list-that-have-a-service-running-on-each-pc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!