Monday, November 17, 2014

Remote diagnostics brief Windows server using Powershell

In this article I want to share Powershell script that quickly collects data from the server:
1. Service not running, but with the type of start automatic
2. Errors and warnings from the Application event log and System
3. The disk space
4. The average CPU load
In most cases, this is sufficient information to verify the server.

Download BriefCheck.ps1

Use the script as follows:
1. Powershell run as an administrator of the domain or a specific server
2. Run .\BriefCheck.ps1 <servername>

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $True, Position = 0)]
    [string]$pc
)

write-host "...Service error:" -fore Green
$pc_s = Get-wmiobject win32_service -Filter "startmode = 'auto' AND state != 'running'" -ComputerName $pc | select DisplayName, name, startname, exitcode
if ($pc_s) { Out-Host -InputObject $pc_s }
else { write-host "no errors" }

write-host "...Eventlog Application,System warnings, errors:" -fore Green
$pc_e = Get-WinEvent -computername $pc -Logname Application, System -MaxEvents 100 -EA silentlycontinue | where-object { $_.timecreated -gt [datetime]::Now.AddHours(-1) -and (($_.LevelDisplayName -eq "Error") -or ($_.LevelDisplayName -eq "Warning")) }
if ($pc_e) { Out-Host -InputObject $pc_e }
else { write-host "no errors" }

write-host "...Disk space:" -fore Green
$TotalGB = @{ Name = "Capacity(GB)"; expression = { [math]::round(($_.Capacity/ 1073741824), 2) } }
$FreeGB = @{ Name = "FreeSpace(GB)"; expression = { [math]::round(($_.FreeSpace / 1073741824), 2) } }
$pc_v = Get-WmiObject -computer $pc win32_volume
$pc_v | Select Name, Label, $TotalGB, $FreeGB | Format-Table -AutoSize

write-host "...CPU Average Load:" -fore Green
Get-WmiObject win32_processor -computer $pc | Measure-Object -property LoadPercentage -Average | Select Average