Search in:

Powershell Which Computer is a user logged in (with GUI)

Social Media

A variation on the script to find which PC a user is logged into, but with a GUI, so that people who are uncomfortable using powershell, only need to run the .ps1 by right clicking and “run with powershell”

WPCU

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 05/04/2016 12:55
# Generated By: rle
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$WPCU_GUI = New-Object System.Windows.Forms.Form
$Btn_Search = New-Object System.Windows.Forms.Button
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$Results = New-Object System.Windows.Forms.ListBox
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$label2 = New-Object System.Windows.Forms.Label
$PCFilter = New-Object System.Windows.Forms.TextBox
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$label1 = New-Object System.Windows.Forms.Label
$Username = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$searchuser=
{
#TODO: Place custom script here
#Set variables
$progress = 0
$domain = "your domain"
#Get Admin Credentials
Function Get-Login {
CLS
Clear-Host
#Write-Host "Please provide admin credentials (for example DOMAIN\admin.user and your password)"
$Global:Credential = Get-Credential "$domain\administrator"
}
Get-Login

if ($Username.Text -eq "" -or $PCFilter.text -eq "") {
$results.items.add("=======================================")
$results.Items.add("Please check your entries and try again")
$results.items.add("=======================================")
} else {
$Username = $Username.Text
$PC = $PCfilter.Text
Function Get-Username {
Clear-Host
$Global:User = $Username
if ($User -eq $null){
$results.items.add("===================================================")
$results.Items.add("Username cannot be blank, please re-enter username!")
$results.items.add("===================================================")
Get-Username
}
$UserCheck = Get-ADUser $User
if ($UserCheck -eq $null){
$results.items.add("=====================================================================")
$results.Items.add("Invalid username, please verify this is the logon id for the account!")
$results.items.add("=====================================================================")
Get-Username
}
}
Get-Username

Function Get-Prefix {
Clear-Host
$Global:Prefix = $PC
Clear-Host
}
Get-Prefix

#Start search
$computers = Get-ADComputer -Filter {Enabled -eq 'true' -and SamAccountName -like $Prefix}
$CompCount = $Computers.Count
$results.Items.clear()
$results.Items.add("Searching for $User on $Prefix on $CompCount Computers`n")
$results.items.add("============================================================")

$f = 0
#Start main foreach loop, search processes on all computers
foreach ($comp in $computers){
$Computer = $comp.Name
$Reply = $null
$Reply = test-connection $Computer -count 1 -quiet
if($Reply -eq 'True'){
if($Computer -eq $env:COMPUTERNAME){
#Get explorer.exe processes without credentials parameter if the query is executed on the localhost
$proc = gwmi win32_process -ErrorAction SilentlyContinue -computer $Computer -Filter "Name = 'explorer.exe'"
}
else{
#Get explorer.exe processes with credentials for remote hosts
$proc = gwmi win32_process -ErrorAction SilentlyContinue -Credential $Credential -computer $Computer -Filter "Name = 'explorer.exe'"
}
#If $proc is empty return msg else search collection of processes for username
if([string]::IsNullOrEmpty($proc)){
$results.Items.add("$user not found on $Computer!")
}
else{
$progress++
ForEach ($p in $proc) {
$temp = ($p.GetOwner()).User
if ($temp -eq $User){
$results.items.add("===============================")
$results.items.add("$User found logged on $Computer")
$results.items.add("===============================")
$f = 1
}
}
}
}
}
if ($f -gt 0) {
$results.items.add("====================================")
$results.items.add("Search done, Username '$user' Found :D")
} else {
$results.items.add("========================================")
$results.items.add("Search done, Username '$user' Not Found :(")
}

}
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$WPCU_GUI.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 571
$System_Drawing_Size.Width = 452
$WPCU_GUI.ClientSize = $System_Drawing_Size
$WPCU_GUI.DataBindings.DefaultDataSourceUpdateMode = 0
$WPCU_GUI.Name = "WPCU_GUI"
$WPCU_GUI.Text = "Which PC is User logged into..?"

$Btn_Search.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 321
$System_Drawing_Point.Y = 524
$Btn_Search.Location = $System_Drawing_Point
$Btn_Search.Name = "Btn_Search"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 36
$System_Drawing_Size.Width = 119
$Btn_Search.Size = $System_Drawing_Size
$Btn_Search.TabIndex = 3
$Btn_Search.Text = "Search"
$Btn_Search.UseVisualStyleBackColor = $True
$Btn_Search.add_Click($searchuser)

$WPCU_GUI.Controls.Add($Btn_Search)

$groupBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 164
$groupBox3.Location = $System_Drawing_Point
$groupBox3.Name = "groupBox3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 354
$System_Drawing_Size.Width = 427
$groupBox3.Size = $System_Drawing_Size
$groupBox3.TabIndex = 2
$groupBox3.TabStop = $False
$groupBox3.Text = "Results"

$WPCU_GUI.Controls.Add($groupBox3)
$Results.BackColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$Results.DataBindings.DefaultDataSourceUpdateMode = 0
$Results.ForeColor = [System.Drawing.Color]::FromArgb(255,51,153,255)
$Results.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 21
$Results.Location = $System_Drawing_Point
$Results.Name = "Results"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 316
$System_Drawing_Size.Width = 406
$Results.Size = $System_Drawing_Size
$Results.TabIndex = 0
$Results.TabStop = $False

$groupBox3.Controls.Add($Results)

$groupBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 90
$groupBox2.Location = $System_Drawing_Point
$groupBox2.Name = "groupBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 62
$System_Drawing_Size.Width = 429
$groupBox2.Size = $System_Drawing_Size
$groupBox2.TabIndex = 1
$groupBox2.TabStop = $False
$groupBox2.Text = "Computer name filter"

$WPCU_GUI.Controls.Add($groupBox2)
$label2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 43
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 16
$System_Drawing_Size.Width = 263
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 1
$label2.Text = "Enter filter (e.g. *data)"

$groupBox2.Controls.Add($label2)

$PCFilter.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 19
$PCFilter.Location = $System_Drawing_Point
$PCFilter.Name = "PCFilter"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 407
$PCFilter.Size = $System_Drawing_Size
$PCFilter.TabIndex = 0

$groupBox2.Controls.Add($PCFilter)

$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 11
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 70
$System_Drawing_Size.Width = 431
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "User Details"

$WPCU_GUI.Controls.Add($groupBox1)
$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 45
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 18
$System_Drawing_Size.Width = 220
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Enter username to search for"

$groupBox1.Controls.Add($label1)

$Username.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 22
$Username.Location = $System_Drawing_Point
$Username.Name = "Username"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 409
$Username.Size = $System_Drawing_Size
$Username.TabIndex = 0

$groupBox1.Controls.Add($Username)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $WPCU_GUI.WindowState
#Init the OnLoad event to correct the initial state of the form
$WPCU_GUI.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$WPCU_GUI.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

Leave a Reply

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