1.

Solve : Shutdown remote computer if uptime is over a period of time?

Answer»

I need to SHUTDOWN workstations with windows 7 operating system if uptime is over a some period of time. I got uptime remotely with: Code: [Select]systeminfo /s "somecomputer" | find "System Boot Time:". Shutdown computer with: Code: [Select]shutdown /m \\"somecomputer" /s. Idea is to make a batch FILE that will compare the CURRENT time and uptime, calculated the difference and if the difference is more than some time period, turn off workstation. Batch file will be executed from Task Scheduler.
Any help will be appreciatedDunno if this can help you at all. There's a routine to calculate time difference though.

Maybe WMIC can be run on a remote machine, or use PSEXEC to run the WMIC command.

Code: [Select]@echo off
for /f "delims=" %%a in ('wmic OS Get LastBootUpTime ^| find "."') do set up=%%a
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a

set beg=%up:~0,4%-%up:~4,2%-%up:~6,2%T%up:~8,2%-%up:~10,2%-%up:~12,2%-%up:~15,3%
set end=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%T%dt:~8,2%-%dt:~10,2%-%dt:~12,2%-%dt:~15,3%


call :TimeDifference uptime %end% %beg%

echo %beg%
echo %end%
echo Uptime is: %uptime%


pause
goto :eof

:TimeDifference Return_Variable Start_Date_Time Stop_Date_Time
:: Version -0 2009-12-25 Frank P. Westlake
:: Calculate the difference in time between parameter 2 and parameter 3
:: and return the values in the variable named by parameter 1.
::
:: Parameters 2 and 3 are ISO8601 DATE/TIMEs of the format
::
:: YYYY-MM-DDThh-mm-ss
::
:: where '-' may be any of '/:-., '.
::
:: RETURN:
:: The variable named by %1 will be set with a string containing each of
:: the following values seperated by spaces:
::
:: DAYS HOURS MINUTES SECONDS MILLISECONDS
::
:: EXAMPLE: Call :TimeDifference diff "2009-12-01T 1:00:00.00" "2009-11-30T13:00:00.01"
:: Sets variable "DIFF=0 12 0 0 10"
SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=1-14 delims=T/:-., " %%a in ("%~2 %~3") Do (
Set "h2=0%%d" & Set "h3=0%%k" & Set "n2=%%g00" & Set "n3=%%n00"
Set /A "y2=%%a, m2=1%%b-100, d2=1%%c-100, h2=1!h2:~-2!-100, i2=1%%e-100, s2=1%%f-100, n2=1!n2:~0,3!-1000"
Set /A "y3=%%h, m3=1%%i-100, d3=1%%j-100, h3=1!h3:~-2!-100, i3=1%%l-100, s3=1%%m-100, n3=1!n3:~0,3!-1000"
)
Set /A "t2=((h2*60+i2)*60+s2)*1000+n2, t3=((h3*60+i3)*60+s3)*1000+n3"
Set /A "a=(14-m2)/12, b=y2-a, j2=(153*(12*a+m2-3)+2)/5+d2+365*b+b/4-b/100+b/400"
Set /A "a=(14-m3)/12, b=y3-a, j3=(153*(12*a+m3-3)+2)/5+d3+365*b+b/4-b/100+b/400"
Set /A "d=j3-j2, t=t3-t2"
If %d% LEQ 0 (If %d% LSS 0 (Set /A "d=j2-j3, t=t2-t3") Else If %t% LSS 0 (Set /A "t=t2-t3"))
If %t% LSS 0 (Set /A "t+=(1000*60*60*24), d-=1")
Set /A "n=t %% 1000, t/=1000, s=t %% 60, t/=60, m=t %% 60, t/=60"
EndLocal & Set "%~1=%d% %t% %m% %s% %n%"
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::
What were you planning on using this for? I can't see much of a practical use other than if someone forgets to turn off the station, but then you'd want to turn them off at a set time (Closing+2h or something).I am an ADMINISTRATOR in the 24-hour active organization, whole year and most computers got same business hours. I wish I had a tool on network that will be started eg. between Saturday and Sunday, when the job INTENSITY is the lowest and restart only computers that need it (usually employed care and reboot them periodically) . Restart for all computers is not acceptable Quote from: foxidrive on September 01, 2012, 08:51:37 AM

Dunno if this can help you at all. There's a routine to calculate time difference though.

Maybe WMIC can be run on a remote machine, or use PSEXEC to run the WMIC command.



Working like charm on local machine. With some customisations shutdown local machine after 7 days look like:

Code: [Select]@echo off
for /f "delims=" %%a in ('wmic OS Get LastBootUpTime ^| find "."') do set up=%%a
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a

set beg=%up:~0,4%-%up:~4,2%-%up:~6,2%T%up:~8,2%-%up:~10,2%-%up:~12,2%-%up:~15,3%
set end=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%T%dt:~8,2%-%dt:~10,2%-%dt:~12,2%-%dt:~15,3%


call :TimeDifference uptime %end% %beg%

if %uptime% gtr 7 shutdown /s /f
goto :eof

:TimeDifference Return_Variable Start_Date_Time Stop_Date_Time
:: Version -0 2009-12-25 Frank P. Westlake
:: Calculate the difference in time between parameter 2 and parameter 3
:: and return the values in the variable named by parameter 1.
::
:: Parameters 2 and 3 are ISO8601 DATE/TIMEs of the format
::
:: YYYY-MM-DDThh-mm-ss
::
:: where '-' may be any of '/:-., '.
::
:: RETURN:
:: The variable named by %1 will be set with a string containing each of
:: the following values seperated by spaces:
::
:: DAYS HOURS MINUTES SECONDS MILLISECONDS
::
:: EXAMPLE: Call :TimeDifference diff "2009-12-01T 1:00:00.00" "2009-11-30T13:00:00.01"
:: Sets variable "DIFF=0 12 0 0 10"
SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=1-14 delims=T/:-., " %%a in ("%~2 %~3") Do (
Set "h2=0%%d" & Set "h3=0%%k" & Set "n2=%%g00" & Set "n3=%%n00"
Set /A "y2=%%a, m2=1%%b-100, d2=1%%c-100, h2=1!h2:~-2!-100, i2=1%%e-100, s2=1%%f-100, n2=1!n2:~0,3!-1000"
Set /A "y3=%%h, m3=1%%i-100, d3=1%%j-100, h3=1!h3:~-2!-100, i3=1%%l-100, s3=1%%m-100, n3=1!n3:~0,3!-1000"
)
Set /A "t2=((h2*60+i2)*60+s2)*1000+n2, t3=((h3*60+i3)*60+s3)*1000+n3"
Set /A "a=(14-m2)/12, b=y2-a, j2=(153*(12*a+m2-3)+2)/5+d2+365*b+b/4-b/100+b/400"
Set /A "a=(14-m3)/12, b=y3-a, j3=(153*(12*a+m3-3)+2)/5+d3+365*b+b/4-b/100+b/400"
Set /A "d=j3-j2, t=t3-t2"
If %d% LEQ 0 (If %d% LSS 0 (Set /A "d=j2-j3, t=t2-t3") Else If %t% LSS 0 (Set /A "t=t2-t3"))
If %t% LSS 0 (Set /A "t+=(1000*60*60*24), d-=1")
Set /A "n=t %% 1000, t/=1000, s=t %% 60, t/=60, m=t %% 60, t/=60"
EndLocal & Set "%~1=%d% "
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::Batch file need to run like administrator. I'm not tested this on network, but i think only difference is in:
Code: [Select]for /f "delims=" %%a in ('wmic/ Node:"computername" OS Get LastBootUpTime ^| find "."') do set up=%%a
for /f "delims=" %%a in ('wmic/ Node:"computername" OS Get localdatetime ^| find "."') do set dt=%%a
and
Code: [Select]if %uptime% gtr 5 shutdown /m \\computername /r /f
Thanks, it's very useful


Discussion

No Comment Found