Friday, February 13, 2009

PowerShell: OS Statistics for a Remote Host

Here's a small PowerShell Script that would enable you to get the OS Statistics for a Host...
----------OS-Statistics.ps1------------------
Function Main {
# Load the Winforms assembly
[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
# Create the form
$form = New-Object Windows.Forms.Form
#Set the dialog title
$form.text = "OS Statistics"














# Create the label control and set text, size and location
$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 50,50
$label.Size = New-Object Drawing.Point 200,15
$label.text = "Enter Hostname"

$combo = new-object System.Windows.Forms.ComboBox
$combo.Location = new-object System.Drawing.Size(50,80)
$combo.Size = new-object System.Drawing.Size(200,25)
$combo.Items.Add("ComputerName / IP")
$combo.Items.Add("192.168.219.")
$combo.Items.Add("127.0.0.1")

# Create Button and set text and location
$button = New-Object Windows.Forms.Button
$button.text = "Get OS Statistics"
$button.Location = New-Object Drawing.Point 50,130
$button.Size = New-Object Drawing.Point 200,25

$button3 = New-Object Windows.Forms.Button
$button3.text = "Quit...."
$button3.Location = New-Object Drawing.Point 50,180
$button3.Size = New-Object Drawing.Point 200,25

# Set up event handler to extarct text from TextBox and display it on the Label.
$button.add_click({ NewForm $combo.text })
$button3.add_click({ $form.dispose() })

# Add the controls to the Form
$form.controls.add($button)
$form.controls.add($button3)
$form.controls.add($label)
$form.controls.add($combo)

# Display the dialog
$form.ShowDialog()
}

Function NewForm
{
Param ($ip)

$ALive=get-wmiobject win32_pingstatus -Filter "Address='$ip'" Select-Object statuscode
if($ALive.statuscode -ne 0)
{
[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
$form1 = New-Object Windows.Forms.Form
$form1.height = 180
$label1 = New-Object Windows.Forms.Label
$label1.Location = New-Object Drawing.Point 50,10
$label1.Size = New-Object Drawing.Point 200,50
$label1.text = "Sorry!! The Host is Unreachable `n`n Check if" + $ip + " is Online..."
$button2 = New-Object Windows.Forms.Button
$button2.text = "Click to Get Back"
$button2.Location = New-Object Drawing.Point 50,80
$button2.Size = New-Object Drawing.Point 200,25
$button2.add_click({
$form1.dispose()
})
$form1.controls.add($button2)
$form1.controls.add($label1)
$form1.showdialog()
}
























else{
[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
$form1 = New-Object Windows.Forms.Form
$form1.height = 600
$form1.width = 300
$label1 = New-Object Windows.Forms.Label
$label1.Location = New-Object Drawing.Point 50,10
$label1.Size = New-Object Drawing.Point 200,600
$perf= get-wmiobject Win32_PerfFormattedData_PerfOS_System -computer $ip
$uptime=$perf.SystemUpTime/3600
$freedisk = get-wmiobject win32_logicaldisk -filter "DriveType=3" -computer $ip foreach-object {"`n{0} {1}" -f $_.DeviceID,($_.freespace/1gb)}
$result = "`n--------------------------------------------`n" + "OS STATISTICS :: " + $ip + "`n--------------------------------------------`n`n" + "Processes:`t" + $perf.Processes + "`n`nThreads:`t" + $perf.Threads + "`n`nSystem UpTime In hours:`t" + $uptime + "`n`nAlignment Fixups/sec:`t" + $perf.AlignmentFixupsPersec + "`n`nContext Switches/sec:`t" + $perf.ContextSwitchesPersec + "`n`nException Dispatches/sec:`t" + $perf.ExceptionDispatchesPersec + "`n`nFile Control Bytes/sec:`t" + $perf.FileControlBytesPersec + "`n`nFile Control Operations/sec:`t" + $perf.FileControlOperationsPersec + "`n`nFile Data OperationsPersec:`t" + $perf.FileDataOperationsPersec+ "`n`nFile Read Bytes/sec:`t" + $perf.FileReadBytesPersec + "`n`nFile Read Operations/sec:`t" + $perf.FileReadOperationsPersec + "`n`nFile Write Bytes/sec:`t"+ $perf.FileWriteBytesPersec +"`n`nFile Write Operations/sec:`t" + $perf.FileWriteOperationsPersec + "`n`nSystem Calls/sec:`t"+ $perf.SystemCallsPersec + "`n`nFree Disk Space in GB:`n" + $freedisk
$label1.text = "$result"
$button1 = New-Object Windows.Forms.Button
$button1.text = "Click to Get Back"
$button1.Location = New-Object Drawing.Point 50,520
$button1.Size = New-Object Drawing.Point 200,25
$button1.add_click({
$form1.dispose()
})
$form1.controls.add($button1)
$form1.controls.add($label1)
$form1.showdialog()
}

}

Main

No comments: