foreach ($server in 'SERVER1','SERVER2')
{
#Test WMI Connection
$testCon = 0
$testWmi = Get-WmiObject Win32_OperatingSystem -computerName $server
if (-not $testWmi) { $testCon += 1; write-host "Cannot connect to WMI on remote server, $server" }
$myCol = @()
#Get WMI info
$wmi=Get-WmiObject -class Win32_OperatingSystem -computer $server -ErrorAction SilentlyContinue
$Time=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime=New-TimeSpan $Time $(get-date)
$uptimeDays = $uptime.days
$uptimeHours = $uptime.hours
$uptimeMins = $uptime.minutes
$Object = "" | Select Server,Days,Hours,Minutes
$Object = New-Object -TypeName psobject
$Object | Add-Member -MemberType noteproperty -Name Server $server
$Object | Add-Member -MemberType noteproperty -Name "Days" $uptimeDays
$Object | Add-Member -MemberType noteproperty -Name "Hours" $uptimeHours
$Object | Add-Member -MemberType noteproperty -Name "Minutes" $uptimeMins
$myCol += $Object
Write-Output $myCol
}
Write-Host "Completed Uptime Check"
No comments:
Post a Comment