Link to home
Start Free TrialLog in
Avatar of Intelli-Seeker
Intelli-SeekerFlag for United States of America

asked on

Get all machines that a user is logged onto

I had this question after viewing Get logged on user for multiple machines with Poweshell. Does anyone have a suggestion to modify this script or a suggestion for a different script that will search by username to find all machines where that user is logged on? This is the code that was in the previous article.

[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$True, Position=0)]
	[PSObject[]]$ComputerName = @($ENV:ComputerName)
)
Begin {
}
Process {
	$ComputerName |
		ForEach-Object {
			Try {
				If ($_ -is [Microsoft.ActiveDirectory.Management.ADComputer]) {
					$ServerName = $_.Name
				} Else {
					$ServerName = $_.ToString()
				}
				Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" -ComputerName $ServerName -ErrorAction Stop |
					Select-Object -Property `
						@{Name="ServerName"; Expression={$_.PSComputerName}},
						@{Name="UserName"; Expression={$_.GetOwner() | % {"$($_.Domain)\$($_.User)"}}},
						@{Name="LogonDate"; Expression={$_.ConvertToDateTime($_.CreationDate)}},
						@{Name="SessionId"; Expression={[UInt32]$_.SessionId}},
						Exception |
					Group-Object -Property SessionId |
					ForEach-Object {
						$_.Group |
							Sort-Object -Property LogonDate |
							Select-Object -First 1
					}
			} Catch {
				$_ | Select-Object -Property `
					@{Name="ServerName"; Expression={$ServerName}},
					UserName,
					LogonDate,
					SessionId,
					@{Name="Exception"; Expression={$_.Exception.Message}}
			}
		}
}
End {
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial