Quantcast
Channel: Microsoft – JeffOps
Viewing all articles
Browse latest Browse all 120

Find users currently logged on to remote systems

$
0
0

This is something I’ve been asked at multiple customers… can you find out who is logged on to a certain system?
We have an IP address or system name, but have no clue where the device is so we want to know who to call.

For this, you can use the following function which utilizes a CIM class to find out who is logged on:

function Get-LoggedOnUser {
    [cmdletbinding()]
    param (
        [parameter(
            mandatory=$true,
            position=0,
            ValueFromPipeline=$true
        )]
        [string[]]$ComputerName
    )
    begin {
    } process {
        foreach ($Computer in $ComputerName) {
            try {
                Get-CimInstance -ClassName Win32_LoggedOnUser -ComputerName $Computer |
                    Select-Object -Property __SERVER, Antecedent -Unique | foreach {
                        New-Object -TypeName PSObject -Property @{
                            'Domain' = $_.Antecedent.ToString().Split('"')[1]
                            'UserName' = $_.Antecedent.ToString().Split('"')[3]
                        }
                    }
            } catch {
                Write-Error $_
            }
        }
    } end {
    }
}

Viewing all articles
Browse latest Browse all 120

Trending Articles