Skip to main content

Powershell and WMI to calculate the Windows server uptime

Use the following PowerShell commands to find out how long the server has been up for since the last restart.

PS C:\> $wmi = Get-WmiObject -Class Win32_OperatingSystem
PS C:\> $wmi.ConvertToDateTime($wmi.LocalDateTime) –
$wmi.ConvertToDateTime($wmi.LastBootUpTime)

To display the uptime in formatted output, use the following commands:

PS C:\Windows\system32> $name = "bcdpprd01"
PS C:\Windows\system32> $wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $name
PS C:\Windows\system32> $uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
PS C:\Windows\system32> Write-Output "Uptime for $($name): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"

Output will be displayed as:

Uptime for bcdpprd01: 0 Day(s), 15 Hour(s), 2 Minute(s), 4 Second(s)

To check the uptime for multiple Windows Servers, create a text file with a list of servers and create a Powershell script with a loop construct like foreach. The beauty of the following script is that it does not require uptime.exe to check uptime for any Windows Server(s).

$servers = Get-Content "C:\Nirav Store\servers.txt"
@(
foreach ($server in $servers)
{
if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = Get-WmiObject -Class win32_operatingsystem -ComputerName $server

$uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)

Write-Output "Uptime for $($server): $($uptime.Days) Day(s), $($uptime.hours) Hour(s), $($uptime.minutes) Minute(s), $($uptime.seconds) Second(s)"
}
Else
{
Write-output "$server is not pingable."
}
}
) | Out-file -FilePath "c:\Nirav Store\results.txt"


If you do not want to redirect the output to a text file, do not pipe the output to a text file, which means, omit “| Out-file -FilePath "c:\Nirav Store\results.txt"”.

Comments

Popular posts from this blog

Microsoft Azure Administrator (AZ-104) Course in Hindi

 Below in the link for the YouTube Video series for the Microsoft Azure Administrator (AZ-104) course in Hindi. https://www.youtube.com/watch?v=fqnsqJoWyjM This video is the first video in this series teaching you the Microsoft Azure Administrator (AZ-104) course in Hindi. I hope, you will enjoy this series, and I am sure that it will help you prepare for your Microsoft Azure Administrator (AZ-104) certification.  If you are enjoying my videos, please like and share them. Please also subscribe to my channel to get notified about the new videos that I publish. Thank you, Nirav Soni

Working with Server Core machine

Before I talk about the Server Core, I would, first of all, list the editions of Windows Server 2012 R2. There are 4 editions in Windows Server 2012 R2. Data Center Standard Essentials Foundation They can be installed with two states - either GUI or without GUI i.e. Server Core. Actually, there's also a third state here and it's kind of the middle ground between Server Core and Server with the GUI and that is the Minimal Server Interface. Server Core is being installed mostly for Domain Controllers, DHCP, Hyper-V and DNS. Server Core only has Command Line and PowerShell. All of those user interface elements that you're used to in Server with GUI like the Microsoft Management Console, Server Manager, Explorer, all of those things are gone. The whole idea here is that it contains a much smaller footprint. It's actually four gigabytes smaller than server with a GUI. This is going to reduce the potential attack surface too. So, it's smaller and more secure....

Challenges with WinSXS folder taking more disk space on Windows Server 2008/2008R2 and solution for it in Windows Server 2012/2012 R2 with new "Features on Demand"

I have seen many Systems Administrators having challenges in cleaning up WinSXS folder on system drive of Windows Servers 2008/2008R2. What is this WinSXS folder for? What does it contain within it? Is it safe to clean up those files under this folder by selecting them and pressing Delete button to get rid of them and reclaim disk space on C: drive? I know, you might be having these questions, if you would have come across the incidents where this folder would be taking lot of space of C: drive. I have seen Windows Server 2008 R2 machines where this folder would be taking over 20GB of disk space. Microsoft has released few knowledge-base articles as well showing how you can deal with these kind of situation. Microsoft realized the pain that Systems Administrators have cleaning up this folder and hence, they came up with "Features on Demand" on Windows Server 2012/2012 R2. So, now, you would be curious to know what this "Features on Demand" is and how it can help ove...