Skip to content

Commit 65baa25

Browse files
committed
Tune remainder of Initialize-ADTModule for performance.
Module initialisation now occurs in ~0.5 seconds. PowerShell 5: ``` [2025-11-30T13:39:56.7150854+11:00] [Initialization] [Open-ADTSession] [Info] :: [PSAppDeployToolkit] module imported in [0.9259279] seconds. [2025-11-30T13:39:56.7150854+11:00] [Initialization] [Open-ADTSession] [Info] :: [PSAppDeployToolkit] module initialized in [0.5725523] seconds. ``` PowerShell 7: ``` [2025-11-30T13:38:25.8503548+11:00] [Initialization] [Open-ADTSession] [Info] :: [PSAppDeployToolkit] module imported in [0.9905205] seconds. [2025-11-30T13:38:25.8520642+11:00] [Initialization] [Open-ADTSession] [Info] :: [PSAppDeployToolkit] module initialized in [0.467705] seconds. ```
1 parent 95f0fe7 commit 65baa25

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

src/PSAppDeployToolkit/Private/Get-ADTClientServerUser.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Private:Get-ADTClientServerUser
5353
}
5454
else
5555
{
56-
Get-ADTRunAsActiveUser 4>$null
56+
Get-ADTRunAsActiveUser
5757
}
5858

5959
# Return the calculated RunAsActiveUser if we have one.

src/PSAppDeployToolkit/Private/Get-ADTRunAsActiveUser.ps1

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,17 @@ function Private:Get-ADTRunAsActiveUser
5454

5555
# Determine the account that will be used to execute client/server commands in the user's context.
5656
# Favour the caller's session if it's found and is currently an active user session on the device.
57-
Write-ADTLogEntry -Message 'Finding the active user session on this device.'
5857
foreach ($session in $userSessions)
5958
{
6059
if ($session.SID.Equals([PSADT.AccountManagement.AccountUtilities]::CallerSid) -and $session.IsActiveUserSession)
6160
{
62-
Write-ADTLogEntry -Message "The active user session on this device is [$($session.NTAccount)]."
6361
return $session.ToRunAsActiveUser()
6462
}
6563
}
6664

6765
# The caller SID isn't the active user session, try to find the best available match.
6866
if ($session = $userSessions | & { process { if ($_.IsActiveUserSession) { return $_ } } } | Sort-Object -Property LogonTime -Descending | Select-Object -First 1)
6967
{
70-
Write-ADTLogEntry -Message "The active user session on this device is [$($session.NTAccount)]."
7168
return $session.ToRunAsActiveUser()
7269
}
73-
Write-ADTLogEntry -Message 'There was no active user session found on this device.'
7470
}

src/PSAppDeployToolkit/Private/New-ADTEnvironmentTable.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ function Private:New-ADTEnvironmentTable
290290
$variables.Add('IsNetworkServiceAccount', $variables.CurrentProcessSID.IsWellKnown([System.Security.Principal.WellKnownSidType]::NetworkServiceSid))
291291
$variables.Add('IsServiceAccount', ($variables.CurrentProcessToken.Groups -contains ([System.Security.Principal.SecurityIdentifier]'S-1-5-6')))
292292
$variables.Add('IsProcessUserInteractive', [System.Environment]::UserInteractive)
293-
$variables.Add('LocalSystemNTAccount', (ConvertTo-ADTNTAccountOrSID -WellKnownSIDName LocalSystemSid -WellKnownToNTAccount -LocalHost 4>$null).Value)
294-
$variables.Add('LocalUsersGroup', (ConvertTo-ADTNTAccountOrSID -WellKnownSIDName BuiltinUsersSid -WellKnownToNTAccount -LocalHost 4>$null).Value)
295-
$variables.Add('LocalAdministratorsGroup', (ConvertTo-ADTNTAccountOrSID -WellKnownSIDName BuiltinAdministratorsSid -WellKnownToNTAccount -LocalHost 4>$null).Value)
293+
$variables.Add('LocalSystemNTAccount', [PSADT.AccountManagement.AccountUtilities]::GetWellKnownSid([System.Security.Principal.WellKnownSidType]::LocalSystemSid).Translate([System.Security.Principal.NTAccount]))
294+
$variables.Add('LocalUsersGroup', [PSADT.AccountManagement.AccountUtilities]::GetWellKnownSid([System.Security.Principal.WellKnownSidType]::BuiltinUsersSid).Translate([System.Security.Principal.NTAccount]))
295+
$variables.Add('LocalAdministratorsGroup', [PSADT.AccountManagement.AccountUtilities]::GetWellKnownSid([System.Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid).Translate([System.Security.Principal.NTAccount]))
296296
$variables.Add('SessionZero', $variables.IsLocalSystemAccount -or $variables.IsLocalServiceAccount -or $variables.IsNetworkServiceAccount -or $variables.IsServiceAccount)
297297

298298
## Variables: Logged on user information
299-
$variables.Add('LoggedOnUserSessions', [System.Collections.Generic.IReadOnlyList[PSADT.TerminalServices.SessionInfo]][System.Collections.ObjectModel.ReadOnlyCollection[PSADT.TerminalServices.SessionInfo]][PSADT.TerminalServices.SessionInfo[]](Get-ADTLoggedOnUser 4>$null))
299+
$variables.Add('LoggedOnUserSessions', [System.Collections.Generic.IReadOnlyList[PSADT.TerminalServices.SessionInfo]][PSADT.TerminalServices.SessionManager]::GetSessionInfo())
300300
if ($variables.LoggedOnUserSessions)
301301
{
302302
$variables.Add('usersLoggedOn', [System.Collections.Generic.IReadOnlyList[System.Security.Principal.NTAccount]][System.Collections.ObjectModel.ReadOnlyCollection[System.Security.Principal.NTAccount]][System.Security.Principal.NTAccount[]]$variables.LoggedOnUserSessions.NTAccount)

src/PSAppDeployToolkit/Public/Complete-ADTFunction.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,8 @@ function Complete-ADTFunction
5252
)
5353

5454
# Write debug log messages and restore original global verbosity if a value was archived off.
55-
Write-ADTLogEntry -Message 'Function End' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
55+
if ($InformationPreference -notmatch '^(SilentlyContinue|Ignore)$')
56+
{
57+
Write-ADTLogEntry -Message 'Function End' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
58+
}
5659
}

src/PSAppDeployToolkit/Public/Initialize-ADTFunction.ps1

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,18 @@ function Initialize-ADTFunction
8989
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
9090

9191
# Write debug log messages.
92-
Write-ADTLogEntry -Message 'Function Start' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
93-
if ($Cmdlet.MyInvocation.BoundParameters.Count)
92+
if ($InformationPreference -notmatch '^(SilentlyContinue|Ignore)$')
9493
{
95-
$CmdletBoundParameters = $Cmdlet.MyInvocation.BoundParameters | Format-Table -Property @{ Label = 'Parameter'; Expression = { "[-$($_.Key)]" } }, @{ Label = 'Value'; Expression = { $_.Value }; Alignment = 'Left' }, @{ Label = 'Type'; Expression = { if ($_.Value) { $_.Value.GetType().Name } }; Alignment = 'Left' } -AutoSize -Wrap | Out-String -Width ([System.Int32]::MaxValue)
96-
Write-ADTLogEntry -Message "Function invoked with bound parameter(s):`n$CmdletBoundParameters" -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
97-
}
98-
else
99-
{
100-
Write-ADTLogEntry -Message 'Function invoked without any bound parameters.' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
94+
Write-ADTLogEntry -Message 'Function Start' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
95+
if ($Cmdlet.MyInvocation.BoundParameters.Count)
96+
{
97+
$CmdletBoundParameters = $Cmdlet.MyInvocation.BoundParameters | Format-Table -Property @{ Label = 'Parameter'; Expression = { "[-$($_.Key)]" } }, @{ Label = 'Value'; Expression = { $_.Value }; Alignment = 'Left' }, @{ Label = 'Type'; Expression = { if ($_.Value) { $_.Value.GetType().Name } }; Alignment = 'Left' } -AutoSize -Wrap | Out-String -Width ([System.Int32]::MaxValue)
98+
Write-ADTLogEntry -Message "Function invoked with bound parameter(s):`n$CmdletBoundParameters" -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
99+
}
100+
else
101+
{
102+
Write-ADTLogEntry -Message 'Function invoked without any bound parameters.' -Source $Cmdlet.MyInvocation.MyCommand.Name -DebugMessage
103+
}
101104
}
102105

103106
# Amend the caller's $ErrorActionPreference to archive off their provided value so we can always stop on a dime.

src/PSAppDeployToolkit/Public/Initialize-ADTModule.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function Initialize-ADTModule
8585
}
8686
$PSCmdlet.ThrowTerminatingError((New-ADTErrorRecord @naerParams))
8787
}
88-
Initialize-ADTFunction -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
88+
Initialize-ADTFunction -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState -InformationAction SilentlyContinue
8989
$null = $PSBoundParameters.Remove('ScriptDirectory')
9090
}
9191

@@ -160,6 +160,6 @@ function Initialize-ADTModule
160160

161161
end
162162
{
163-
Complete-ADTFunction -Cmdlet $PSCmdlet
163+
Complete-ADTFunction -Cmdlet $PSCmdlet -InformationAction SilentlyContinue
164164
}
165165
}

0 commit comments

Comments
 (0)