Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. Import-Module activedirectory
  2. write-host "**************************************************************"
  3. Write-Host "`nThis script will tell you what computer a user is logged in to" -ForegroundColor Yellow -BackgroundColor Black
  4. write-host "`n**************************************************************"
  5.  
  6. $i = 0
  7.  
  8. #sets up credentials
  9. $myusername = "**DOMAIN**\" + [Environment]::UserName
  10.  
  11.  
  12. $credentials = Get-Credential -UserName $myusername -Message "Please enter your ADMIN password"
  13. write-host "`nPlease enter a user name to query:" -ForegroundColor Yellow
  14. $name = read-host
  15. $username = ((get-aduser -filter * | where {$_.name -like "$name"} | select samaccountname).samaccountname).tostring()
  16.  
  17. # Gets relevant servers with shares:
  18. write-host "`nGetting server list." -ForegroundColor Cyan
  19. $servers = get-adcomputer -filter {(name -like "*KSLNAS01*") -or (name -like "kslpartner") -or (name -like "kslsql01*") -or (name -like "kslfs01")} | sort-object name
  20.  
  21.  
  22. # Starts a job for each server
  23. write-host ""
  24. write-host 'Querying' $servers.count 'servers.' -ForegroundColor Yellow
  25. $servercount = $servers.count
  26. foreach ($server in $servers)
  27. {
  28. # Querying each of the servers for connections to shares
  29. $servername = $server.name
  30. $userSessions = Get-WmiObject Win32_ServerConnection -computername $servername -Credential $credentials
  31.  
  32. #progress bar
  33. write-host "Querying $servername" -ForegroundColor DarkYellow
  34.  
  35. Write-Progress -activity "Servers Processed" -status "Percent Complete : " -PercentComplete (($i++ / $Servers.Length) * 100)
  36.  
  37. # Filtering out the results
  38. foreach ($userSession in $userSessions)
  39. {
  40.  
  41. if ($userSession.username -eq $username)
  42. {
  43. #convert ip to hostname
  44. $IPAddress = $userSession.computername
  45. $hostname = Get-WmiObject win32_computersystem -Computer $IPAddress -Credential $credentials
  46.  
  47. $found = "yes"
  48.  
  49. #select user details
  50. $userDetails = [string]::Format("User {0} is logged on to {1}", $userSession.UserName, $hostname.name)
  51.  
  52. #print user details
  53. Write-Host "`n$userDetails" -ForegroundColor Green
  54. $continue = read-host "`nPress ENTER to exit"
  55.  
  56. if ($continue -Ne "C")
  57. {
  58. exit
  59. }
  60.  
  61. }
  62.  
  63.  
  64. else
  65. {
  66. #if no instances are found, continue to search next server
  67. }
  68.  
  69. }
  70.  
  71. }
  72.  
  73. if ($found -ne "yes")
  74. {
  75. write-host "I can't find any PCs with $user logged in to them" -ForegroundColor Red
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement