Guest User

Find what computer a user is logged into

a guest
Apr 19th, 2015
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
  2. $ErrorActionPreference = "SilentlyContinue"
  3.  
  4. Function Get-Username {
  5. $Global:Username = Read-Host "Enter username you want to search for"
  6. if ($Username -eq $null){
  7. Write-Host "Username cannot be blank, please re-enter username!"
  8. Get-Username}
  9. $UserCheck = Get-QADUser -SamAccountName $Username
  10. if ($UserCheck -eq $null){
  11. Write-Host "Invalid username, please verify this is the logon id for the account"
  12. Get-Username}
  13. }
  14.  
  15. get-username
  16.  
  17. $computers = Get-QADComputer | where {$_.accountisdisabled -eq $false}
  18. foreach ($comp in $computers)
  19. {
  20. $Computer = $comp.Name
  21. $ping = new-object System.Net.NetworkInformation.Ping
  22. $Reply = $null
  23. $Reply = $ping.send($Computer)
  24. if($Reply.status -like 'Success'){
  25. #Get explorer.exe processes
  26. $proc = gwmi win32_process -computer $Computer -Filter "Name = 'explorer.exe'"
  27. #Search collection of processes for username
  28. ForEach ($p in $proc) {
  29. $temp = ($p.GetOwner()).User
  30. if ($temp -eq $Username){
  31. write-host "$Username is logged onto $Computer"
  32. }}}}
Advertisement
Add Comment
Please, Sign In to add comment