Get logged on user for multiple machines with Poweshell

MilesLogan
MilesLogan used Ask the Experts™
on
Hi EE

Does anyone have a script that I can use for this ? I used the script the below to check the the UserProfile time and filtered by today's
date .. but I am getting mixed results .. so not sure if this is accurate .. wondering what every one is using ?

Get-WmiObject -ComputerName (get-content C:\PS\Machines.txt) Win32_UserProfile | % {
  $userinfo = [WMI] ($_.__Path -Replace "Win32_UserProfile", "Win32_SID")
  New-Object PsObject -Property @{
    ComputerName= $_.__Server
    Domain      = $userinfo.ReferencedDomainName
    User        = $userinfo.AccountName
    LastUsed    = $_.ConvertToDatetime($_.LastUseTime)
  }
} | export-csv -NoType C:\PS\test_Data.csv

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I tried this and it does return the logged on user but I cant get it to output the PSComputerName or CSNAme value .. so the csv file can have one column with the user and the other with the corresponding machine name

Get-WmiObject Win32_Process -ComputerName MachineName | Where {$_.name -eq 'explorer.exe'}| Invoke-WmiMethod -name GetOwner | select user

Open in new window


heeeeelp
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this script; you can pipe the computer names to it, or pass them with the -ComputerName argument.
For machines where an error occurred during the query, it will have a column "Exception" which will show the error message.
[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

Author

Commented:
Thanks oBdA ! .. worked realy well

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial