Posted in CIM, Powershell

Better way to Reboot a computer

I saw a post of how to use the restart-computer cmdlet. They used wait to pause the process until the computer was back online. This post is just how to take it one step further. If I want my script to wait for the reboot to occur it is normally because I want to perform more actions against it. In this post I am using the restart-computer cmdlet but I am extending it to wait but I am also specifying what it will wait For.  In this case I am waiting for WinRM to come online so that I can connect to CIM.

Yes, I splatted it because I love splatting parameters for readability and flexibility.

#Requires -Version 5
<# .SYNOPSIS     Better way to reboot computers .DESCRIPTION     Really it is just an demonstration of the flexibility of restart-computer. Being able to tell the cmdlet to     wait for a specific element to be online before continuing can help with workflows that require a reboot and     then further action. .PARAMETER ServerName     Name of the computer to be restarted. .EXAMPLE     C:\PS>.\better-rebootcomputer.ps1 -servername testmachine
    Example of how to use this cmdlet
    Created with: 	Visual Studio Code
    Created on:   	05.01.2017
    Created by:   	John Kavanagh
    Organization: 	TekSystems
    Filename:     	better-rebootcomputer.ps1
    date initials: Enter first comment here

     [Parameter(Mandatory = $true,
     ValueFromPipeline = $true,
     Position = 1,
     HelpMessage = "Enter the computer name to be rebooted")]
     [ValidateScript({test-connection -Computername $_ -Count 1 -Quiet })]
$args = @{
    ComputerName = $servername
    Wait         = $True
    For          = "WinRM"
    Force        = $True
restart-computer @args
# Now let's test - yes I am using CIM so For has to be WinRM, WMI is up first but CIM
# requires WinRM
$sess = New-CimSession -computer $servername
Get-CimInstance -classname win32_operatingsystem -CimSession $sess<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>




USMC, CVID, Technology, PowerShell... just general ramblings

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s