10.9 C
New York
Saturday, January 13, 2024

Tremendous Compact, DevOps-ish Pending Reboot Check for The Rebootiest Working System in The Cloud


Home windows and reboots – quite a lot of candles have been burned on each ends in understanding and resolving this relationship. Prefer it or not and regardless of Microsoft’s efforts – Home windows is essentially the most rebootiest working system round.

There needs to be a ton of code written round this – is it attainable add a brand new contribution of actual worth?

I believe it’s – by being concise round my particular context of software program deployment automation for DevOps within the cloud with a brutal eye to compactness.

Get-PendingReboot Heritage and A New Reboot Detection

Fairly a while in the past I created an earlier model of this code with among the core reboot situations being drawn from the lengthy standing Get-PendingReboot resolution by bcwwilhite. To optimize for my state of affairs, that code was severely compacted.

Just lately I needed to go to it once more because the aim behind the automation I used to be writing was to detect {that a} reboot is pending resulting from eradicating Home windows Defender whereas putting in an alternate resolution. I discovered that Get-PendingReboot doesn’t detect this sort of pending reboot. The code included right here is up to date to have the ability to detect that reboot by the contents of a file. (Word that it will also be detected by doing an uninstall-windowsfeature on a function you realize has already been eliminated and the return worth will present you whether or not a beforehand triggered reboot is pending – however that’s arduous to put in writing as a normal resolution that may by no means do one thing nasty.)

What do I imply “DevOps Deployment Automation”?

Once I engineer DevOps options I’ve a powerful orientation to creating bits of code which can be compact sufficient to reside inside the script they’re utilized in. In actual fact, the core aim is often a single script. This enables flexibility like simply operating instantly from a supply code repository and straightforward distribution as inline code for orchestration techniques that assist it as a result of the necessity to push a file can add an entire new stage of complexity to the automation.

For essentially the most half DevOps Software program Deployment Automation runs instantly on the node that’s being automated – so a part of the compaction was to eradicate Get-PendingReboot’s capability to execute remotely. As a normal resolution, distant operation is a improbable function – but it surely provides a considerable quantity of code.

Moreover, Get-PendingReboot contained vital code to deal with checking for SCCM reboots and whereas that is additionally an ideal function for a normal resolution, I’ve not seen SCCM used for software program deployment in a DevOps toolchain – so it was eliminated.

Compacting Code: Smallness and Embedding Over Type and Readability

Compacting code tacks strongly towards measurement over typical model and readability tips. I do that as a result of:

  1. the code can be included so often in so many issues and
  2. resulting from it’s simplicity, updates needs to be uncommon.

Compacting and together with additionally avoids shared code hell by testing and isolating it to every resolution that makes use of the template code. If the code is embedded (a type of isolation) and labored below testing of a given automation resolution, then updates to the template code won’t break current automation as a result of it should by no means be utilized by earlier options that leveraged it. Even when the code isn’t embedded, it needs to be remoted. If the code is in a script file, it needs to be dot supply or if a module, loaded by file identify from the native location – with this isolation methodology you’ll keep away from putting in modules into any shared module folder on the goal machine.

By not sharing code throughout automation options, the liberty and suppleness from backward compatibility engineering and regression testing is really liberating.

Remember I construct developer automation tooling in a SaaS firm that has 10’s of 1000’s of situations, 1000’s of builders and tons of of merchandise. I wouldn’t have the posh of assuming or managing all situations to a typical set of runtimes. Nor do I’ve the posh of understanding what extra tooling has taken a dependency on my code.

Nevertheless, I’ve taken the identical “favor isolation” strategy in environments the place I did have the power to handle it extra carefully and there have been nonetheless large advantages to favoring code isolation.

The Code

The under Check-PendingReboot code is the compacted operate. In order for you it to be extra compact, you possibly can take away the feedback. A pattern utilization can also be given.

Operate Check-PendingReboot
 Choose -Broaden 'ComputerName') -ine (get-itemproperty 'HKLM:SYSTEMCurrentControlSetControlComputerNameComputerName' 

# Pattern Utilization
If (Check-PendingReboot)
{ Write-Host "Shutting down in 10 seconds (giving time for orchestrating automation to shut out)..."
  shutdown.exe /r /t 10 }
Else {Write-Host "A reboot isn't pending, no motion taken"}

CompactDevOpsRebootWindowsIfNeeded.ps1



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles