Skip to content

Instantly share code, notes, and snippets.

@Ath3na-UK
Forked from DanGough/Invoke-ServiceUI.ps1
Created April 24, 2024 10:04
Show Gist options
  • Save Ath3na-UK/67984c6cf9a85ca73fd5699ddcae39b9 to your computer and use it in GitHub Desktop.
Save Ath3na-UK/67984c6cf9a85ca73fd5699ddcae39b9 to your computer and use it in GitHub Desktop.
Stub script for launching PSADT with ServiceUI only if a specific exe (explorer.exe by default) is running
<#
.SYNOPSIS
This is a helper script to launch Deploy-Application.exe via ServiceUI to force the process to become visible when deployed by Intune, or other deployment systems that run in session 0.
.DESCRIPTION
By default it will invoke ServiceUI if explorer.exe is running and the current process is non-interactive. An alternate ProcessName can be specified if you only want the toolkit to be visible when a specific application is running.
.PARAMETER ProcessName
Specifies the name of the process check for to trigger the interactive installation. Default value is 'explorer'. Multiple values can be supplied such as 'app1','app2'. The .exe extension must be omitted.
.PARAMETER DeploymentType
Specifies the type of deployment. Valid values are 'Install', 'Uninstall', or 'Repair'. Default value is 'Install'.
.PARAMETER AllowRebootPassThru
Passthru of switch to Deploy-Application.exe, will instruct the toolkit to not to mask a 3010 return code with a 0.
.PARAMETER TerminalServerMode
Passthru of switch to Deploy-Application.exe to enable terminal server mode.
.PARAMETER DisableLogging
Passthru of switch to Deploy-Application.exe to disable logging.
.EXAMPLE
.\Invoke-ServiceUI.ps1 -ProcessName 'WinSCP' -DeploymentType 'Install' -AllowRebootPassThru
#>
param (
[string[]]$ProcessName = @('explorer'),
[ValidateSet('Install', 'Uninstall', 'Repair')]
[string]$DeploymentType = 'Install',
[switch]$AllowRebootPassThru,
[switch]$TerminalServerMode,
[switch]$DisableLogging
)
Set-Location $PSScriptRoot
if ($env:PROCESSOR_ARCHITECTURE -eq 'AMD64' -or $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64') {
$Architecture = 'x64'
} else {
$Architecture = 'x86'
}
if (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue) {
if ([Environment]::UserInteractive) {
# Start-Process is used here otherwise script does not wait for completion
$Process = Start-Process -FilePath '.\Deploy-Application.exe' -ArgumentList "-DeploymentType $DeploymentType -DeployMode Interactive -AllowRebootPassThru:`$$AllowRebootPassThru -TerminalServerMode:`$$TerminalServerMode -DisableLogging:`$$DisableLogging" -NoNewWindow -Wait -PassThru
exit $Process.ExitCode
} else {
# Using Start-Process with ServiceUI results in Error Code 5 (Access Denied)
&".\ServiceUI_$Architecture.exe" -process:explorer.exe Deploy-Application.exe -DeploymentType $DeploymentType -DeployMode Interactive -AllowRebootPassThru:"`$$AllowRebootPassThru" -TerminalServerMode:"`$$TerminalServerMode" -DisableLogging:"`$$DisableLogging"
exit $LastExitCode
}
} else {
$Process = Start-Process -FilePath '.\Deploy-Application.exe' -ArgumentList "-DeploymentType $DeploymentType -DeployMode Silent -AllowRebootPassThru:`$$AllowRebootPassThru -TerminalServerMode:`$$TerminalServerMode -DisableLogging:`$$DisableLogging" -NoNewWindow -Wait -PassThru
exit $Process.ExitCode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment