Intelli-Seeker
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 {
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.