Get-LoggedinUser and Get-LoggedInUserSID

Gets the username or user SID of a local or remote computer.

Function Get-LoggedInUser {
<#
.SYNOPSIS
Gets logged in user on local or remote computers.

.NOTES
    Author:  Curtis Wright
    Email:   curtis (at) cwew.co
    Date:    05FEB2018
    PSVer:   2.0/3.0/4.0/5.0

#>
#Allowing Cmdlet Bindings
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
$Computer = $env:COMPUTERNAME,
$domain = $env:userdomain
)
Write-Verbose "Computer is $Computer and domain is $domain"
#Getting the current Error Action Preference so the script can set it back after completion.
$CurrentEA = $ErrorActionPreference
Write-Verbose "Current Error action is $CurrentEA"
$ErrorActionPreference = "SilentlyContinue"
Write-verbose "Attempting to use WMI to get currently logged in user"
$ActiveProcesses = Get-WMIObject win32_process -filter 'name="explorer.exe"' -ComputerName $Computer
if (($ActiveProcesses.GetType().basetype.name) -eq "Array")
    {
        Write-verbose "Object is an Array"
        foreach ($Process in $ActiveProcesses)
            {
                $user = $Process.GetOwner().user
                $sessionusers = (query session $user /server:$Computer)[1]
                if ($sessionusers -like "*Active*")
                    {
                    Write-Verbose "Setting user $user"
                    $loggedinuser = $domain + "\"+ $user
                    }
            }
    }
else
    {
    Write-Verbose "Only one user is detected as logged on"
    $loggedinuser = (Get-WMIObject win32_process -filter 'name="explorer.exe"' -ComputerName $Computer | ForEach-Object {$owner = $_.getowner(); '{0}\{1}' -f $owner.domain, $owner.user} | Sort-Object | Get-Unique).tostring()
    }
$loggedin2 = $loggedinuser.Split("\")
$LoggedIn = $loggedin2[1].TrimEnd()
Write-Verbose "User detected as $LoggedIn"
Write-Output $LoggedIn
}


Function Get-LoggedInUserSID {
<#
.SYNOPSIS
Gets logged in user SID on local or remote computers.

.NOTES
    Author:  Curtis Wright
    Email:   curtis (at) cwew.co
    Date:    05FEB2018
    PSVer:   2.0/3.0/4.0/5.0

#>
#Allowing Cmdlet Bindings
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
$Computer = $env:COMPUTERNAME,
$domain = $env:userdomain
)
Write-Verbose "Computer is $Computer and domain is $domain"
#Getting the current Error Action Preference so the script can set it back after completion.
$CurrentEA = $ErrorActionPreference
Write-Verbose "Current Error action is $CurrentEA"
$ErrorActionPreference = "SilentlyContinue"
Write-verbose "Attempting to use WMI to get currently logged in user"
$ActiveProcesses = Get-WMIObject win32_process -filter 'name="explorer.exe"' -ComputerName $Computer
if (($ActiveProcesses.GetType().basetype.name) -eq "Array")
    {
        Write-verbose "Object is an Array"
        foreach ($Process in $ActiveProcesses)
            {
                $user = $Process.GetOwner().user
                $sessionusers = (query session $user /server:$Computer)[1]
                if ($sessionusers -like "*Active*")
                    {
                    Write-Verbose "Setting user $user"
                    $loggedinuser = $domain + "\"+ $user
                    }
            }
    }
else
    {
    Write-Verbose "Only one user is detected as logged on"
    $loggedinuser = (Get-WMIObject win32_process -filter 'name="explorer.exe"' -ComputerName $Computer | ForEach-Object {$owner = $_.getowner(); '{0}\{1}' -f $owner.domain, $owner.user} | Sort-Object | Get-Unique).tostring()
    }
$loggedin2 = $loggedinuser.Split("\")
$LoggedIn = $loggedin2[1].TrimEnd()
$loggedinusersid = (Get-WmiObject win32_useraccount -ComputerName $Computer -Filter "name = '$loggedin' AND domain = '$domain'").sid
Write-Output $loggedinusersid
}

Comments

Popular posts from this blog

Application Doesn't Install During OSD Task Sequence in SCCM

SCCM PXE Boot Issues - No Advertisements Found

Outlook VBA Macros for Journal Entries