Skip to content

Instantly share code, notes, and snippets.

@danjpadgett
Last active January 11, 2018 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjpadgett/7afd8821534c07ead085d369ce902e3e to your computer and use it in GitHub Desktop.
Save danjpadgett/7afd8821534c07ead085d369ce902e3e to your computer and use it in GitHub Desktop.
Get-RemoteLoggedUser.ps1
$result = @()
$time = 1
$list = (Get-Content C:\Script\List.txt)
function get-LoggedInusers($i)
{
$Username = $null
$explorerprocesses = @(Get-WmiObject -ComputerName $i -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
if ($explorerprocesses.Count -eq 0)
{
} else {
foreach ($i in $explorerprocesses)
{
$Username = $i.GetOwner().User
}
}
if ($Username) {
return $Username
}
else {return 'No Process'}
}
function Get-ComputerSite($i)
{
$site = nltest /server:$i /dsgetsite 2>$null
if($LASTEXITCODE -eq 0){ $site[0] }
}
Foreach ($i in $list)
{
$resData = New-Object System.Object
$resData | Add-Member -type NoteProperty -name ComputerName -value $i
if (Test-Connection -ComputerName $i -Quiet -Count 1)
{
try {
$resData | Add-Member -type NoteProperty -name UserName -value ( get-LoggedInusers $i )
$resData | Add-Member -type NoteProperty -name ADSite -value (Get-ComputerSite $i) }
catch {$resData | Add-Member -type NoteProperty -name SiteCode -value ("Connection Failed : $($error[0].Exception.Message)") -Force}
}
else {$resData | Add-Member -type NoteProperty -name UserName -value ('Connection Failed : Ping Failed')}
$result += $resData
Write-Progress -activity "Checking logged in user for $($list.length) clients" -status "Percent checked: " -PercentComplete (($time / $list.length) * 100)
$time++
}
$result | Export-Csv -Path C:\Script\report.csv -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment