Most of the code deals with the graphic interface.
IMPORTANT! You most likely have to change the Execution Policy from Restricted (default) on every client PC (GPO can do that easily) . Otherwise you won't be able to run (any) Powershell script. Use Get-ExecutionPolicy and Set-ExecutionPolicy commands if unsure.
Now here's the code:
#creates the window that asks for computername
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Find Logged On Users on a PC"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter PC Name:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
#displays the window that asks for computername
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
#THIS IS WHERE THE FUN PART STARTS
#queries DNS for the computername
#this will be used mostly to error-check any typos in computername
$IPis = ""
$IPis = [System.Net.Dns]::GetHostAddresses($x)
if ($IPis -eq "")
{ $Message = "Host cannot be resolved in DNS"}
else
{
#host is valid; now check if it is online by pinging it
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($x)
if ($Reply.status –eq "Success")
{
#Host is online so check if a user is logged in
$Bulk = gwmi -class win32_computerSystem -computer:$x | Select-Object username
if ($Bulk.username.Length -gt 0)
{$Message = $Bulk.username}
else
{ $Message = "No user is logged in locally"}
}
else
{ $Message = "Host not online (ping failed)"}
}
#displays the result
$a = new-object -comobject wscript.shell
$b = $a.popup($Message,0,"Logged In User",1)