PureGremlin

Get users on all servers

Dec 6th, 2021 (edited)
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Username in Samaccount name form
  2. $user = "username"
  3.  
  4. Function Get-LoggedInuser {
  5.  
  6. <#
  7.     .SYNOPSIS
  8.         Shows all the users currently logged in to specified or remote computers
  9.  
  10.     .DESCRIPTION
  11.         Shows the users currently logged into the specified computername if not specified local computers will be shown.
  12.  
  13.     .PARAMETER ComputerName
  14.         One or more computernames
  15.  
  16.     .EXAMPLE
  17.         PS C:\> Get-LoggedInUser
  18.         Shows the users logged into the local system
  19.  
  20.     .EXAMPLE
  21.         PS C:\> Get-LoggedInUser -Computer server1,server2,server3
  22.         Shows the users logged into server1, server2, and server3
  23.  
  24.     .EXAMPLE
  25.         PS C:\> Get-LoggedInUser  | where{$_.sessionname -match 'RDP'}  ## shows where users are connected VIA RDP
  26.  
  27.     .Example
  28.  
  29.         PS C:\> Get-ADComputer -Filter *|select -exp name |Foreach{Get-LoggedInUser -Computer $_ }
  30.         Shows all computers in active directory computers
  31.  
  32.     .Example
  33.  
  34.         PS C:\> get-content C:\listofcomputers.txt |Foreach{Get-LoggedInUser -Computer $_ }
  35.         Shows all computers in the text file.
  36.  
  37.     .Example
  38.        
  39.         PS C:\> Get-LoggedInUser -computer server1,server2  | where{$_.username -eq 'admin'}
  40.        Shows where a user islogged in to.
  41.  
  42.     .MORE
  43.          [PSCustomObject]@{
  44.           ComputerName    = $Comp
  45.           Username        = $line.SubString(1, 20).Trim()
  46.           SessionName     = $line.SubString(23, 17).Trim()
  47.           #ID             = $line.SubString(42, 2).Trim()
  48.           State           = $line.SubString(46, 6).Trim()
  49.           #Idle           = $line.SubString(54, 9).Trim().Replace('+', '.')
  50.           #LogonTime      = [datetime]$line.SubString(65)
  51.  
  52.           Remove # Against each if you want to see the Output of the Value
  53.      
  54. ************************************************************************
  55.  Notes: Run as administrator
  56.  Author: Jiten https://community.spiceworks.com/people/jitensh
  57. Date Created: 08/30/2018
  58.      Credits:
  59. Last Revised: 08/30/2018
  60. ************************************************************************
  61. #>
  62.  
  63. [cmdletbinding()]
  64.    param(
  65.    [String[]]$Computer = $env:COMPUTERNAME
  66.    )
  67.  
  68.    ForEach ($Comp in $Computer)
  69.    {
  70.    If (-not (Test-Connection -ComputerName $comp -Quiet -Count 1 -ea silentlycontinue))
  71.    {
  72.    Write-Warning "$comp is Offline"; continue
  73.    }
  74.    $stringOutput = quser /server:$Comp 2>$null
  75.       If (!$stringOutput)
  76.       {
  77.       Write-Warning "Unable to retrieve quser info for `"$Comp`""
  78.       }
  79.       ForEach ($line in $stringOutput){
  80.          If ($line -match "logon time")
  81.          {Continue}
  82.  
  83.          [PSCustomObject]@{
  84.           ComputerName    = $Comp
  85.           Username        = $line.SubString(1, 20).Trim()
  86.           SessionName     = $line.SubString(23, 17).Trim()
  87.           ID             = $line.SubString(42, 2).Trim()
  88.           State           = $line.SubString(46, 6).Trim()
  89.           #Idle           = $line.SubString(54, 9).Trim().Replace('+', '.')
  90.           #LogonTime      = [datetime]$line.SubString(65)
  91.           }
  92.          
  93.       }
  94.    }
  95. }
  96.  
  97. $serv=Get-ADComputer -Filter {operatingsystem -like "*Server*"}
  98. $s=@()
  99. $serv | % {$s+=$_.name}
  100. Get-LoggedInuser -Computer $s | ?{$_.username -like $user}
Add Comment
Please, Sign In to add comment