0

We have a problem with people in our company leaving their computer running 24/7 even over weekends. Our corporate policy is to shut down when not in use but that doesn't always happen. So what we have been doing is running a simple script using PsInfo to pull back the uptime of the computers. We export out the list of computers from AD into a text file then use a batch file to go through the list, pull the uptime info, and save it to another text file:

For /F "tokens=*" %%i in (ComputerList.txt) do psinfo uptime -nobanner \\%%i  1>>UptimeResults.txt

Works great but if a computer is not online the default timeout seems to be 60 seconds which can cause the script to take a very long time for hundreds of computers. PsExec itself has a timeout switch you can use but PsInfo doesn't seem to take this into account. Is there another way to either force it to timeout after say 10 seconds or a different program that will do the same thing? Possibly using PsExec directly with the time out switch and some other command? As a side note we have already disabled fastboot so the uptime results are accurate.

Things we have debated but are hoping for a easier solution: Using PsExec and running systeminfo | find "Boot Time" then analyze the boot time and do some math to get runtime. Getting a old copy of uptime.exe, deploying it to the computers, and using it's output with PsExec. And switching over to a powershell command that would poll WMI for last boot time and again do the math. All seem overly complicated and would rather have the easy but slow solution unless someone else has a better suggestion.

2
  • 2
    Why not use WMI to get the LastBootupTime? FOR /F %i IN (File.txt) DO WMIC /NODE:"%i" /FAILFAST OS GET LastBootupTime
    – Greg Askew
    Jan 13 '20 at 19:32
  • PsInfo gives a simple output of uptime and we don't have to deal with any math to compare to boot time. The results are scanned over quickly by a actual person right now so seeing the uptime in days quickly is helpful. I.e. the output of the PsInfo command in my original post is something like this: System information for \\PC4221DA: Uptime: 4 days 23 hours 55 minutes 48 seconds
    – Allan
    Jan 15 '20 at 14:26
2

I like Greg's suggestion to get the last bootup time.

But I believe the following will do what you asked. I use WMIC to launch PSINFO within a new cmd.exe session. FOR /F is used to capture the PID of the new CMD session. Then I call a routine to monitor the process for up to 10 seconds. If it is no longer running, then return immediately. But after 10 seconds I kill the process before returning.

I tested the launch and monitor process with a dummy TIMEOUT process. But I could not test with PSINFO because I do not have PSINFO, nor do I have your environment.

@echo off
setlocal
set "timeout=10"
set "log=UptimeResults.txt"

:: Start with an empty result file
copy /y nul %log% >nul

:: Loop through the list of computers
for /f "tokens=*" %%i in (ComputerList.txt) do (
  %= For each computer, launch PSINFO via wmic and use FOR /F to capture the parent cmd.exe session PID =%
  %= The cd argument is the location where the process should run (current directory) =%
  for /f "tokens=2 delims=;= " %%P in (
    'wmic process call create 'cmd /c "psinfo uptime -nobanner \\%%i 1>>%log%"'^, "%cd%" ^| find "ProcessId"'
  ) do call :monitor %%P 2>nul 1>nul
)
exit /b

:monitor  PID
:: Monitor up to %timeout% seconds and return immediately if process no longer running
for /l %%N in (1 1 %timeout%) do (
  timeout 1
  tasklist /fi "pid eq %1" | find "cmd.exe" || exit /b
)
:: It has been longer than %timeout% seconds, so kill the process
taskkill /pid %1 /f /t
exit /b
1
  • So first the bad: Since you are spawning new processes they pop up constantly and take focus while the original scrip, while slow, could just be minimized and would chug along in the background. But the good: Your solution works as intended, gives the same results I had before, and allows it to timeout. And since it is running so quick it could be done at lunch and be well finished before getting back which negates the spawning issue. Marked as answer, thank-you.
    – Allan
    Jan 15 '20 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.