Use Get-LoggedOnUsers Function without needing the whole appdeploymenttoolkit

Hi,

I have been looking for a function that will display all sessions on a machine. The function provided within PSADT has everything i need however i want to make it a standalone function without having to dot source the toolkit since i need to use it outside of application deployment.

I can see the function itself refers to the CS file which has all the commands in it.

Any help is most appreciated

Normally I wouldn’t help for non-PSADT related stuff but since you are not a new user and I saw this coming from Reddit, why not this one time.

It’s not trivial but you might find a way to shrink it some more.
CAVEAT: Not tested.

#TODO: path to AppDeployToolkitMain.cs
[String]$appDeployCustomTypesSourceCode = '<path to>\AppDeployToolkitMain.cs'

#CAVEAT: If you need to change AppDeployToolkitMain.cs you'll need to change the class name or reboot. I don't think you can remove Types.
## Add the custom types required for the toolkit
If (-not ([Management.Automation.PSTypeName]'PSADT.UiAutomation').Type) {
 # Not sure if you need these other assemblies, uncomment if it fails.
 #   [String[]]$ReferencedAssemblies = 'System.Drawing', 'System.Windows.Forms', 'System.DirectoryServices'
 #   Add-Type -Path $appDeployCustomTypesSourceCode -ReferencedAssemblies $ReferencedAssemblies -IgnoreWarnings -ErrorAction 'Stop'
	Add-Type -Path $appDeployCustomTypesSourceCode -IgnoreWarnings -ErrorAction 'Stop'
}

#region Function Get-LoggedOnUser
Function Get-LoggedOnUser {
    <#
.SYNOPSIS
Get session details for all local and RDP logged on users.

.DESCRIPTION
Get session details for all local and RDP logged on users using Win32 APIs. Get the following session details:
    NTAccount, SID, UserName, DomainName, SessionId, SessionName, ConnectState, IsCurrentSession, IsConsoleSession, IsUserSession, IsActiveUserSession
    IsRdpSession, IsLocalAdmin, LogonTime, IdleTime, DisconnectTime, ClientName, ClientProtocolType, ClientDirectory, ClientBuildNumber

.INPUTS
None

You cannot pipe objects to this function.

.OUTPUTS
None

This function does not return any objects.

.EXAMPLE
Get-LoggedOnUser

.NOTES
Description of ConnectState property:

Value        Description
-----        -----------
Active       A user is logged on to the session.
ConnectQuery The session is in the process of connecting to a client.
Connected    A client is connected to the session.
Disconnected The session is active, but the client has disconnected from it.
Down         The session is down due to an error.
Idle         The session is waiting for a client to connect.
Initializing The session is initializing.
Listening    The session is listening for connections.
Reset        The session is being reset.
Shadowing    This session is shadowing another session.

Description of IsActiveUserSession property:

- If a console user exists, then that will be the active user session.
- If no console user exists but users are logged in, such as on terminal servers, then the first logged-in non-console user that has ConnectState either 'Active' or 'Connected' is the active user.

Description of IsRdpSession property:
- Gets a value indicating whether the user is associated with an RDP client session.

Description of IsLocalAdmin property:
- Checks whether the user is a member of the Administrators group

.LINK
https://psappdeploytoolkit.com
#>
    [CmdletBinding()]
    Param (
    )

        Try {
            Write-host -Message 'Getting session information for all logged on users.'
            Write-Output -InputObject ([PSADT.QueryUser]::GetUserSessionInfo("$env:ComputerName"))
        }
        Catch {
			[String]$MyErrorMessage = @{'Message:' = "$($_.Exception.Message)";'ScriptStackTrace:'="$($_.ScriptStackTrace)";'InnerException:'="$($_.Exception.InnerException)"} `
	| Format-Table -AutoSize -Wrap -HideTableHeaders | Out-String
            Write-host -Message "Failed to get session information for all logged on users. `r`n$MyErrorMessage"
        }

}
#endregion


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.