16 March 2022

Last Server Reboot Reporting

Recently, we needed a report of the last boot time of all servers. I wrote this PowerShell script that queries AD for a list of all windows servers and then does a WMI query on each server for the LastBootUpTime. It then calculates the number of days and writes this to an object with the computer name and the number of days since the last reboot. It will write this info to a CSV file. 

You can download the script from my GitHub site.

 Import-Module -Name ActiveDirectory -Force  
 #Get list of windows based servers  
 $Servers = Get-ADComputer -Filter * -Properties * | Where-Object {$_.OperatingSystem -like '*windows server*'} | Select Name | Sort-Object -Property Name  
 #Create Report Array  
 $Report = @()  
 #Parse through server list  
 Foreach ($Server in $Servers) {  
   #Get the computer name  
   $ComputerName = ([String]$Server).Split("=")[1].Split("}")[0].Trim()  
   #Check if the system is online  
   If ((Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) -eq $true) {  
     #Query last bootup time and use $Null if unobtainable  
     Try {  
       $LastBootTime = (Get-CimInstance -ClassName win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue).LastBootUpTime  
       $LastBoot = (New-TimeSpan -Start $LastBootTime -End (Get-Date)).Days  
     } Catch {  
       $LastBoot = $null  
     }  
     #Add computername and last boot time to the object  
     If ($ComputerName -ne $null) {  
       $SystemObject = New-Object -TypeName psobject  
       $SystemObject | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName  
       $SystemObject | Add-Member -MemberType NoteProperty -Name DaysSinceLastBoot -Value $LastBoot  
       $Report += $SystemObject  
     }  
   } else {  
       $SystemObject = New-Object -TypeName psobject  
       $SystemObject | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName  
       $SystemObject | Add-Member -MemberType NoteProperty -Name DaysSinceLastBoot -Value 'OFFLINE'  
       $Report += $SystemObject  
   }  
   $ComputerName = $null  
 }  
 #Print report to screen  
 $Report  
 $OutFile = "C:\Users\Desktop\LastRebootReport.csv"  
 #Delete CSV file if it already exists  
 If ((Test-Path -Path $OutFile) -eq $true) {  
   Remove-Item -Path $OutFile -Force  
 }  
 #Export report to CSV file  
 $Report | Export-Csv -Path $OutFile -NoClobber -Encoding UTF8 -NoTypeInformation -Force  
   

Related Posts:

  • USMT 4.0 MDT/SCCM Migration This script will execute the USMT, creating a MIG file located on the selected location, either on the local machine, or on the network location. This is intended to be used for generating the MIG file for the MDT/SCCM ima… Read More
  • Using Sysprep to upgrade or replace a failed HDD This script will sysprep the HDD on one machine so that it can be swapped out in another PC. This can save considerable money in shipping when replacing a bad HDD or upgrading the OS in a remote office. You can use any mod… Read More
  • Automate Unmounting WIM Files This script will allow you to painlessly mount WIM files for editing. It runs relative to its location it was executed from. Here is the link to download this script. '***************************************************… Read More
  • Editing Revit INI Files Here is a script that will make custom changes to the revit.ini file on user machines. You first open the script up and define the changes needed in the EditINIFiles subroutine. Currently, there are 4 changes I have in the … Read More
  • Automate Mounting WIM Files This script will allow you to painlessly mount WIM files for editing. It runs relative to its location it was executed from. Here is the link to download the file. '******************************************************… Read More

0 comments:

Post a Comment