Blitzcrank
Blitzcrank

Reputation: 967

How to out put the PC name that has certain process running?

I m using psexec to auto run

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


on all PCs on our network,

how can i get the result with PC name instead of only pmill.exe in the text file? is there anyway i can do from powershell? I need to get the pc name in result. Hint plz!

Upvotes: 0

Views: 109

Answers (1)

rojo
rojo

Reputation: 24466

Instead of psexec, try this:

@echo off
setlocal enabledelayedexpansion
for /f %%I in (pclist.txt) do (
    set /p q="Checking %%I... "<NUL
    ping -n 1 -w 500 %%I>NUL 2>NUL
    if !errorlevel!==1 (
        echo Offline.
    ) else (
        wmic /node:%%I /user:adminuser /password:pass process where name="pmill.exe" get csname 2>NUL | find /i "%%I" >>dc-01\c$\0001.txt
        echo Done.
    )
)

That'll output %computername% if pmill.exe is running, or nothing otherwise.

Edit:

If you must use psexec then I suggest changing the logic of your for loop that calls psexec, something like this:

@echo off
setlocal enabledelayedexpansion
for /f %%I in (pclist.txt) do (
    set pc=%%I
    set /p q="Checking %%I... "<NUL
    ping -n 1 -w 500 %%I>NUL 2>NUL
    if !errorlevel!==1 (
        echo Offline.
    ) else (
        for /f %%z in ('psexec \\!pc! -u adminuser -p pass tasklist 2^>^&1 ^| findstr /i "pmill.exe"') do (
            set /p q="pmill.exe found.  "<NUL
            echo !pc!>>dc-01\c$\0001.txt
        )
        echo Done.
    )
)

Upvotes: 2

Related Questions