<# .Synopsis Query Uptime Details of servers. .Description This script helps you to get the uptime details of the servers. It also generates a HTML report when -HTMLReport switch is used. The report contains the uptime details and a summary of how many computers reachable and how many are not .Parameter ComputerName Computer name(s) for which you want to get the uptime details. .Parameter HTMLReport Generates a HTML report in c:\ drive with name uptimereport.html by default. You can override this by specifying -HTMLFile parameter .Parameter HTMLFile Name of the file path where you want to store the report .Example Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2 Gets the Uptime of Comp1 and Comp2 .Example Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2 -HTMLReport Get the uptime of Comp1 and Comp2 and saves the report in HTML format .Example Get-Content c:\servers.txt | Get-UptimeOfServers.ps1 -HTMLReport Get the uptime of computers listed in servers.txt and saves the report in HTML format .Notes NAME: Get-UptimeOfServers.ps1 AUTHOR: Sitaram Pamarthi WEBSITE: http://techibee.com #> [cmdletbinding()] param( [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:computername, [switch]$HTMLReport, [string]$HTMLFile = "c:\Uptimereport.html" ) begin{ if($HTMLReport) { $Report = " Server Uptime Report

Server Uptime Report


Report Generated at $(Get-Date)

" } } process { $Count=0 $SuccessComps = 0 $UnreachableComps = 0 $FailedComps = 0 $FinalOutput = @() foreach($Computer in $ComputerName) { $Count++ $Computer = $Computer.Toupper() $OutputObj = New-Object -TypeName PSobject $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer $Status = 0 if(Test-Connection -Computer $Computer -count 1 -ea 0) { $OutputObj | Add-Member -MemberType NoteProperty -Name IsOnline -Value "TRUE" try { $Boottime = (Get-WmiObject win32_operatingSystem -computer $Computer -ErrorAction stop).lastbootuptime $Boottime = [System.Management.ManagementDateTimeconverter]::ToDateTime($BootTIme) $Now = Get-Date $span = New-TimeSpan $BootTime $Now $Uptime = "{0} day(s), {1} hour(s), {2} min(s), {3} second(s)" -f $span.days, $span.hours, $span.minutes, $span.seconds $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value $Uptime $Status=1 $SuccessComps++ } catch { $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "FAILED TO GET" $FailedComps++ } } else { $OutputObj | Add-Member -MemberType NoteProperty -Name IsOnline -Value "FALSE" $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "" $UnreachableComps++ } $FinalOutput +=$OutputObj if($HTMLReport) { if($Status) { $BGColor="green" } else { $BGColor="red" } $Report += "" } else { $OutputObj } } } end{ if($HTMLReport) { $Report +="
S. No Server Name IsOnline Status
$Count $($OutputObj.ComputerName) $($OutputObj.IsOnline) $($OutputObj.Uptime)

Report Summary:

Total No. of Computers scanned : $Count
No. Of computers online : $SuccessComps
No. Of computers Offline : $UnreachableComps
No. Of computers Failed to query : $FailedComps
This report is brought to you by http://techibee.com `(Follow`). Visit for more Powershell scripts and System administrator Material
" $Report | Out-File $HTMLFile -Force } }