Font Size

SCREEN

Cpanel

Články

SCCM reboot clients with random time-seed

Here is the goal: reboot more than 5.000 machines using SCCM at a given time. That's pretty easy when talking about servers or desktops. But my target are virtual computers, running on few physical clusters. I cannot simply reboot all of them at the same time since that could cause performance issues on these clusters when the VM's will come back online. The solution is obvious - add some random seed to the restart command.

 

Sure, they are some scripts to archive this goal, but I want to choose the simplest way - just to run BATCH command directly from distribution point.

The Windows CMD shell contains environmental variable %RANDOM% that generates random integer from 0 to 32767, so let's use it:

%SystemRoot%\system32\shutdown.exe /r /t %RANDOM% /d p:02:17

But this does not work in SCCM, why? Looks like that %RANDOM% variable is generated by cmd.exe process, so you can't simply run shutdown.exe with %RANDOM% variable in SCCM command line. The correct syntax is:

cmd.exe /c "%SystemRoot%\system32\shutdown.exe /r /t %RANDOM% /d p:02:17"

 

Time range:

One condition you need to acquiesce with is that this command reboots the computer in between 0 - 32767 seconds, or (32767 / 60 / 60) = 9.1 hours... 

How to hack default time range according your needs? The main idea is visible from this equation: ROUND( (%RANDOM%*MaxTimeToReboot) / 32767 )

Here is the equation converted to the batch code where the maximum time to reboot is 10 minutes (600 seconds): 

SET /a My_RND = (%RANDOM%*600/32767)

SET /a The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. Moreover Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole integer.

 

Putting all together:

The last step is to use %My_RND% variable instead of %RANDOM% in the shutdown command. Let's try:

cmd.exe /c "SET /a My_RND = (%RANDOM%*1000/32767) | %SystemRoot%\system32\shutdown.exe /r /t %My_RND% /d p:02:17"

Again, this does not work. And now it becomes somehow tricky, because some not well-know behavior of batch scripting comes in place.

 

Pipe:

commandA | commandB : Pipe the output from commandA into commandB

The issue why the pipe can not be used is because when a command is piped, everything behind the pipe is executed in a new instance of cmd.exe. And we have set the %My_RND% variable in the "first" instance, it's value is not defined in the new cmd.exe instance. Instead of pipe, I have used && redirection:

commandA && commandB : Run commandA, if it succeeds then run commandB

 

cmd.exe:

I need to solve two issues - run the cmd command and then terminate the cmd itself and force the cmd not to expand the %My_RND% variable at the time of parsing the whole command, but at the time of executing the shutdown command - because the variable is not defined when parsing the whole command. Take a look at the cmd.exe parameters:

/V:ON Enable delayed environment variable expansion using ! as the delimiter. For example, /V:ON would allow !var! to expand the variable var at execution time.  The var syntax expands variables at input time, which is quite a different thing when inside of a FOR loop.
/C Carries out the command specified by string and then terminates   

 

Final batch command:

cmd.exe /v /c "SET /a My_RND = (%RANDOM%*600/32767) && %SystemRoot%\system32\shutdown.exe /r /t !My_RND! /d p:02:17"

 

Are you kidding me?:

On Windows 7 machine, list help for shutdown command and take a look at /t parameter:

 /t xxx Set the time-out period before shutdown to xxx seconds. The valid range is 0-315360000 (10 years), with a default of 30. If the timeout period is greater than 0, the /f parameter is implied.

10 years? Really does someone have a Windows 7 system running for 10 year's without reboot to utilize such a feature? :)

On Windows Server 2003 and 2008 (not R2) the /t parameter shows completely different situation:

 /t xxx   Set the time-out period before shutdown to xxx seconds. The valid range is 0-600, with a default of 30.

 10 minutes? Really that is the maximum time-out period?

 

List of shutdown reasons:

/d parameter of the shutdown command, these reasons are displayed in eventlog.

(E = Expected U = Unexpected P = planned, C = customer defined)

Type Major Minor Title
U 0 0 Other (Unplanned)
E 0 0 Other (Unplanned)
E P 0 0 Other (Planned)
U 0 5 Other Failure: System Unresponsive
E 1 1 Hardware: Maintenance (Unplanned)
E P 1 1 Hardware: Maintenance (Planned)
E 1 2 Hardware: Installation (Unplanned)
E P 1 2 Hardware: Installation (Planned)
2 2 Operating System: Recovery (Planned)
E P 2 2 Operating System: Recovery (Planned)
P 2 3 Operating System: Upgrade (Planned)
E 2 4 Operating System: Reconfiguration (Unplanned)
E P 2 4 Operating System: Reconfiguration (Planned)
P 2 16 Operating System: Service pack (Planned)
  2 17 Operating System: Hot fix (Unplanned)
P 2 17 Operating System: Hot fix (Planned)
  2 18 Operating System: Security fix (Unplanned)
P 2 18 Operating System: Security fix (Planned)
E 4 1 Application: Maintenance (Unplanned)
E P 4 1 Application: Maintenance (Planned)
E P 4 2 Application: Installation (Planned)
E 4 5 Application: Unresponsive
E 4 6 Application: Unstable
U 5 15 System Failure: Stop error
U 5 19 Security issue
E 5 19 Security issue
E P 5 19 Security issue
E 5 20 Loss of network connectivity (Unplanned)
U 6 11 Power Failure: Cord Unplugged
U 6 12 Power Failure: Environment
 P 7 0 Legacy API shutdown

Přidat komentář