Get the server uptime in days with WMI

Get the server uptime in days with WMI

This script gets the Active Directory server uptime in days using WMI. The WMI class used is Win32_OperatingSystem

I have used the New-Timespan to calculate the uptime (difference between the current date and the last Windows boot up time).

[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
This will compare the output time with the current time and store the result to the string as object

The property ConvertToDateTime gives a readable value contained in the variable “LastBootUpTime”.

The default display of the System.TimeSpan object is shown here:

The script below performs the following actions :

  • lists all servers in the Active Directory domain
  • ping each server in the list
  • if the ping is successful, get the uptime of the server calculated in days

Script :

 

Cmdlet references :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social Media Auto Publish Powered By : XYZScripts.com
%d bloggers like this: