How can I automate the Tasklist command on my workstation to check a specific process on X number of servers?

StackOverflow https://stackoverflow.com/questions/9301557

Question

I'm trying to check what the status is on my XenApp servers for the spoolsv.exe process. I've got the command down to run individually from my XP workstation, but can't seem to get it to iterate through a text file. Here's what I have so far, what will make this populate Servers X-XX on my CMD screen?

@echo off

FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO echo tasklist /S %%G /u domain\userid | find "spoolsv.exe"

pause

I can't seem to get it to run correctly, and sometimes it will just pop up my servers.txt file in notepad and not even run. What am I missing?

Was it helpful?

Solution

As you have it presented, tasklist never runs. The "do echo tasklist..." snippet means the literal string "tasklist /S server-one..." is being echo'ed to stdout. Since none of these literal strings contain "spoolsv.exe", the "find" command won't match anything.

Try the following instead:

@echo off

FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO call :RunTasklistForOneServer %%G

pause
goto :EOF

:RunTasklistForOneServer

set ServerName=%1
echo Calling server %ServerName%
tasklist /S %ServerName% /u domain\userid | find "spoolsv.exe"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top