measuring-reboot-time-with-powershell

Measuring Reboot Time with PowerShell

Measuring Reboot Time with PowerShell

In enterprise environments, you will usually find an array of different Windows operating systems, hardware and of course software. Typically, machines are managed and configured with group policy and SCCM, or perhaps other more modern methods like Chef or Puppet. One of the symptoms of an unhealthy Windows computer is the amount of time it takes to boot up.

Longer boot times can mean hardware is failing, such as slow hard disks. Or perhaps, there is a group policy that is holding up the process of getting to the login screen. In this article, I will show how you can use PowerShell to measure the number of seconds a computer(s) takes to go from shutting down and starting up.

Measure-RebootTime Command

A while back, we had a fleet of machines that users were complaining about. The issue was that certain machines were taking a long time to reboot, some even 15 minutes. Since these machines are not used at certain hours or days, I decided to create a PowerShell function to reboot a bunch and measure their reboot time. That is why I created Measure-RebootTime that includes a helper function Get-RebootTime.

The Get-RebootTime function itself is very simple. It used Measure-Command to measure using Restart-Computer while also waiting for PowerShell (WMI) to be available for remote connection. I also set a timeout of 1200 seconds. I then round the seconds to two decimals and throw it into a PSCustomObject.

$Time = Measure-Command {

    Restart-Computer -ComputerName $ComputerName -Wait -For powershell -Timeout 1200 -ErrorAction Stop

} | Select-Object -ExpandProperty TotalSeconds

$RoundedTime = [math]::Round($Time,2)

[PSCustomObject]@{

    ComputerName = $ComputerName

    Seconds = $RoundedTime

}

After creating Get-RebootTime I realized I wanted to make this process multiple computers at once, so I added Measure-RebootTime which uses PoshRSJob to process multiple machines in jobs. PoshRSJob is a great PowerShell community module written by Bo Prox that is widely used for utilizing PowerShell run spaces.

#requires -modules PoshRSJob

function Measure-RebootTime {

    [CmdletBinding()]

    param(

        [Parameter(Mandatory=$true)]

        [string[]]$ComputerName

    )

    $ComputerName | Start-RSJob -ScriptBlock {Get-RebootTime -ComputerName $_ } -Throttle 150 | Wait-RSJob | Get-RSJob | Receive-RSJob | Select-Object *

}
 

Testing It Out

So, in this example, I have two machines, Test-1 and Test-2 that I want to measure the seconds it takes to reboot them. I can simply add them both to the -ComputerName parameter in Measure-RebootTime.

C:\> Measure-RebootTime -ComputerName Test-1,Test-2




ComputerName Seconds

------------ -------

Test-1       51

Test-2       50




As we see, the returned object includes the computer name, and seconds it took to reboot the machine and have WMI accessible over the network. Of course, it is up the user to figure out how much time is acceptable for a reboot operation of a particular machine; in this case, it took about 50 seconds for each.

Summary

The real brains of these PowerShell functions are Measure-Command, Restart-Computer and Start-RSJob. All of which when combined can do this task very efficiently and easily with not much code or complexity involved. Using built-in PowerShell modules and community modules is something I have incorporated into my scripts, which has saved me a lot of time when writing functions.

 

Related Posts


Comments
Comments are disabled in preview mode.
Loading animation