How to check servers Up Time and other system information

Today I want to show how easily check servers Up Time and other useful system information with PowerShell. To get all details we will use Get-WmiObject command.

Get-SystemInfo function will collect system information like:

– Domain
– Organization
– Machine type
– Operating system
– Version
– Build Number
– OSArchitecture
– Up Time
– Network adapter and IPv4 address
– Physical RAM
– C:\ Drive free space
– Memory usage
– CPU usage

To check system up time we need to do a little math:

# Get OS details using WMI query
$os = Get-WmiObject win32_operatingsystem -ComputerName $Server | select LastBootUpTime,LocalDateTime,organization,Caption,OSArchitecture 

$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime(($os).LastBootUpTime)
$LocalDateTime = [Management.ManagementDateTimeConverter]::ToDateTime(($os).LocalDateTime)
                    
# Calculate uptime
$up = $LocalDateTime - $LastBootUpTime
$uptime = "$($up.Days) days, $($up.Hours)h, $($up.Minutes)mins" 

Usage:

Get-SystemInfo
Get-SystemInfo -Servers DC01,ADFS02
Get-SystemInfo -Servers (Get-Content "c:\temp\input.txt")

Final script:


Function Get-SystemInfo {
    [CmdletBinding()]
           
    # Parameters used in this function
    param
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Provide server names", ValueFromPipeline = $true)] 
        $Servers = $env:computername
    ) 
   
    ForEach ($Server in $Servers )
    {
            # Checking if server exist
            Try
            {
                $Check = [System.Net.Dns]::GetHostAddresses($Server) 
            }
            Catch
            {
                $_.Exception.Message
                Break
            }
   
                $ErrorActionPreference = 'SilentlyContinue'
                Write-Host "Processing"$Server": "  -ForegroundColor Yellow -NoNewline
   
                $SystemArray = @()
                   
                    # Adding properties to object
                    $Object = New-Object PSCustomObject
                    $Object | Add-Member -Type NoteProperty -Name "Computer name" -Value $Server
   
                    # Get OS details using WMI query
                    $os = Get-WmiObject win32_operatingsystem -ComputerName $Server | select LastBootUpTime,LocalDateTime,organization,Caption,OSArchitecture,Version,BuildNumber
                 
                    # Get bootup time and local date time  
                    $LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime(($os).LastBootUpTime)
                    $LocalDateTime = [Management.ManagementDateTimeConverter]::ToDateTime(($os).LocalDateTime)
                       
                    # Calculate uptime - this is automatically a timespan
                    $up = $LocalDateTime - $LastBootUpTime
                    $uptime = "$($up.Days) days, $($up.Hours)h, $($up.Minutes)mins"
                   
                    # Get computer system details
                    $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Server
                    
                    # Use switch to determine machine type 
                    switch ($ComputerSystemInfo.Model) 
                    { 
                        # Check for Hyper-V Machine Type 
                        "Virtual Machine" { $MachineType="VM"} 
    
                        # Check for VMware Machine Type 
                        "VMware Virtual Platform" { $MachineType="VM"} 
    
                        # Check for Oracle VM Machine Type 
                        "VirtualBox" { $MachineType="VM"} 
                        
                        # Check for Oracle VM Machine Type 
                        "HVM domU" { $MachineType="Xen"} 
   
                        # Otherwise it is a physical Box 
                        default { $MachineType="Physical"} 
                    } 
   
                    # Add collected properties to object
                    $Object | Add-Member -Type NoteProperty -Name "Domain" -Value $ComputerSystemInfo.domain
                    $Object | Add-Member -Type NoteProperty -Name "Organization" -Value $os.organization
                    $Object | Add-Member -Type NoteProperty -Name "Machine Type" -Value $MachineType
                    $Object | Add-Member -Type NoteProperty -Name "Operating system" -Value $os.Caption
                    $Object | Add-Member -Type NoteProperty -Name "Version" -Value $os.version
                    $Object | Add-Member -Type NoteProperty -Name "Build Number" -Value $os.BuildNumber
                    $Object | Add-Member -Type NoteProperty -Name "OSArchitecture" -Value $os.OSArchitecture
                    $Object | Add-Member -Type NoteProperty -Name "Up Time" -Value $uptime
                       
                       
                    # Checking network adapters and their IP address
                    $NetAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Server -Namespace "root\CIMV2" | where{$_.IPEnabled -eq “True”}
   
                    ForEach($Item in $NetAdapters) 
                    {
                        $NetAdapName = $Item.Description
                        $Object | Add-Member -Type NoteProperty -Name "$NetAdapName" -Value $Item.IPAddress[0]
                    }
  
                    # Checking memory and cpu usage and C: drive free space
                    $PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $server | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)})
                    $PhysicalRAM = ("$PhysicalRAM" + " GB")
                    $Mem = (Get-WmiObject -Class win32_operatingsystem -ComputerName $Server  | Select-Object @{Name = "MemoryUsage"; Expression = { “{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}}).MemoryUsage
                    $Mem = ("$Mem" + " %")
                    $Cpu =  (Get-WmiObject win32_processor -ComputerName $Server  |  Measure-Object -property LoadPercentage -Average | Select Average).Average 
                    $Cpu = ("$Cpu" + " %")
                    $FreeSpace =  (Get-WmiObject win32_logicaldisk -ComputerName $Server  | Where-Object {$_.deviceID -eq "C:"} | select @{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}).freespace 
                    $FreeSpace = ("$FreeSpace" + " GB")
                    $DiskName = (Get-WmiObject win32_logicaldisk -ComputerName $Server | Where-Object {$_.deviceID -eq "C:"}).deviceID
                    $DiskName = ("$DiskName" + " drive free space")
 
 
 
                    # Add collected properties to object
                    $Object | Add-Member -Type NoteProperty -Name "Physical RAM" -Value $PhysicalRAM
                    $Object | Add-Member -Type NoteProperty -Name "$DiskName" -Value $FreeSpace
                    $Object | Add-Member -Type NoteProperty -Name "Memory usage %" -Value $Mem
                    $Object | Add-Member -Type NoteProperty -Name "CPU usage %" -Value $Cpu
                       
                    # Adding object to array
                    $SystemArray += $Object
    
                    # Display results
                    $SystemArray | Format-List
    }
   
}

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.