| 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. Dunno if this can help you at all. There's a routine to calculate time difference though. 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 |
|