Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

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

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-01c$001.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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...