Asset Management Suite

 View Only

Powershell Script to Query Whether a PC is Online and a User is Logged In 

Feb 25, 2011 01:09 PM

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)

Statistics
0 Favorited
0 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Comments

Feb 25, 2011 02:03 PM

If you don't want the GUI, this is also easily done right in the console.  In this example computers.txt is a text list of computernames. All you need is a one line expression.

PS C:\> get-wmiobject win32_computersystem -ComputerName (get-content c:\files\computers.txt) | select name,user

Of if you prefer a little more robustness, only checking computers that you can ping try this one line command.

PS C:\> get-content computers.txt | where {test-connection $_ -quiet} | Foreach {
Get-Wmiobject win32_computersystem -ComputerName $_} | select name,username

 

Jeffery Hicks
Windows PowerShell MVP
JDHITSolutions.com/blog
http://twitter.com/jeffhicks

"Those who neglect to script are doomed to repeat their work."

Now Available: Windows PowerShell 2.0: TFM (SAPIEN Press 2010)

Related Entries and Links

No Related Resource entered.