Ping Last User GUI

PingLastUser

[
void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[
void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

## Some Variables To Use
$ComputerName=“”

## Main Window Size
$objForm=New-ObjectSystem.Windows.Forms.Form
$objForm.Text=“Ping Last User”
$objForm.Size=New-ObjectSystem.Drawing.Size(800,610)
$objForm.StartPosition=“CenterScreen”
$objForm.KeyPreview=$True


## Adding the text box, ok button and label for text box for the tape devices
$objLabel1=New-ObjectSystem.Windows.Forms.Label
$objLabel1.Location=New-ObjectSystem.Drawing.Size(8,20)
$objLabel1.Size=New-ObjectSystem.Drawing.Size(120,15)
$objLabel1.Text=“Enter the hostname:”
$objForm.Controls.Add($objLabel1)

$HostName=New-ObjectSystem.Windows.Forms.TextBox
$HostName.Location=New-ObjectSystem.Drawing.Size(10,40)
$HostName.Size=New-ObjectSystem.Drawing.Size(120,20)
$objForm.Controls.Add($HostName)

## Adding the list box for the tape slots
$objLabel2=New-ObjectSystem.Windows.Forms.Label
$objLabel2.Location=New-ObjectSystem.Drawing.Size(8,65)
$objLabel2.Size=New-ObjectSystem.Drawing.Size(190,17)
$objLabel2.Text=“Results”
$objForm.Controls.Add($objLabel2)

$ResultsBox=New-ObjectSystem.Windows.Forms.ListBox
$ResultsBox.Location=New-ObjectSystem.Drawing.Size(10,85)
$ResultsBox.Size=New-ObjectSystem.Drawing.Size(765,15)
$ResultsBox.Height=480
$ResultsBox.SelectionMode=“MultiExtended”

## Default Text in the Listbox
$SelectionText=“Enter a computer name in the box above…Hit Find User or Hit Enter…”
[
void] $ResultsBox.Items.Add($SelectionText)

## This line adds the listbox to the control(app window).
$objForm.Controls.Add($ResultsBox)

functionDisplayInfo {

$ComputerName=$HostName.Text
$ResultsBox.Items.Clear()
$ResultsBox.Refresh()

foreach($MachineName in $ComputerName)
{
$PingStatus=GwmiWin32_PingStatus -Filter “Address = ‘$MachineName'” |
Select-Object StatusCode

if ($PingStatus.StatusCode -eq 0)
{
$CS=GwmiWin32_ComputerSystem -Comp $MachineName
## Grab info on the machine
$ResultsBox.Forecolor = “green” # Change the font Color of the listbox items
$Str=“Machine Name: “
[
void] $ResultsBox.Items.Add($str)
$str=” “
[
void] $ResultsBox.Items.Add($str)
$Str=” “+$CS.Name+” – “+“Ping Success”
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

$Str=“Currently Logged On User: “
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

$Str=” “+$CS.UserName
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

## go through the IP addresses
$net=Get-WMIObject-class Win32_NetworkAdapterConfiguration -ComputerName ‘
$MachineName
$netenabled=$net | where {$_.IPenabled}
foreach ($NetCard in $netenabled)
{
$Str=$NetCard.Caption
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

$Str=” “+$NetCard.IpAddress[0]
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

}
}

else
{
$Str=“Machine Name: “
[
void] $ResultsBox.Items.Add($str)

$str=” “
[
void] $ResultsBox.Items.Add($str)

$Str=” “+$MachineName+” – “+“Ping Failed”
[
void] $ResultsBox.Items.Add($str)

$ResultsBox.Forecolor=“red”
}

}
$HostName.clear()
$HostName.Focus()

}

$OKButton=New-ObjectSystem.Windows.Forms.Button
$OKButton.Location=New-ObjectSystem.Drawing.Size(135,39)
$OKButton.Size=New-ObjectSystem.Drawing.Size(100,23)
$OKButton.Text=“Find User”
$OKButton.Add_Click(
{
DisplayInfo
})

$objForm.Controls.Add($OKButton)
$objForm.Add_KeyDown(
{
if ($_.KeyCode-eq“Enter”)
{
DisplayInfo
}
}
)

$Closed=“false”
$objForm.Add_KeyDown({if ($_.KeyCode-eq“Escape”)
{
$Closed=“true”;$objForm.Close()}})
$objForm.Topmost=$True
$objForm
.Add_Shown({$objForm.Activate()})
[
void] $objForm.ShowDialog()

Leave a Reply

Your email address will not be published. Required fields are marked *