I'm uninstalling VPN module, and I would like the uninstall to be silent if the user is not logged in, then machine to reboot. I have followed the suggestion in these 2 posts below and added `finally{..} block
this is the code i added after the try{..}catch{..} block
finally
{
if (!(Get-Variable -Name mainErrorMessage -ErrorAction Ignore))
{
Initialize-ADTModule -ScriptDirectory $PSScriptRoot
if ($runAsActiveUser -and $script:namUninstalled)
{
Start-Sleep -Seconds 30 # allow time for user to see toast notification before restart prompt
Show-ADTInstallationRestartPrompt -CountdownSeconds 3600 -Title "Please restart your computer" -Subtitle "NAM module has been uninstalled. Restart your computer to complete the process." -WindowLocation TopCenter
Start-Sleep -Seconds 3630
}
else
{
Show-ADTInstallationRestartPrompt -SilentRestart
}
}
}
When the user is logged on, they receive a prompt to select uninstall/defer , then a restart prompt appears. This works fine, however when no user is logged on and silent uninstall kicks in but the machine does not restart.
Show-ADTInstallationRestartPrompt -SilentRestart doesn't kick in.
If i replace silent restart command with shutdown -r -t 0 the machine does restart. have i missed something not sure how the user got his to work or does it need an active session?
Oh that brings back memories, installing / uninstalling the Cisco AnyConnect / Secure Client NAM module
We did this with PSADT v3.10 previously, supressing the restart until the end of the script
N.B. If you refer to the 1st example I think you are missing -SilentCountdownSeconds 300 from your Show-ADTInstallationRestartPrompt -SilentRestart command, it's probably waiting and doesn't know how long it needs to wait before triggering the restart.
This is specifically when it executes in the finally block. The commands works fine, if it's placed somewhere else either in the post uninstall or before close-adtsession
for context this is where it sits.
# Commence the actual deployment operation.
try
{
# Import any found extensions before proceeding with the deployment.
Get-ChildItem -LiteralPath $PSScriptRoot -Directory | & {
process
{
if ($_.Name -match 'PSAppDeployToolkit\..+$')
{
Get-ChildItem -LiteralPath $_.FullName -Recurse -File | Unblock-File -ErrorAction Ignore
Import-Module -Name $_.FullName -Force
}
}
}
# Invoke the deployment and close out the session.
& "$($adtSession.DeploymentType)-ADTDeployment"
Close-ADTSession
}
catch
{
# An unhandled error has been caught.
$mainErrorMessage = "An unhandled error within [$($MyInvocation.MyCommand.Name)] has occurred.`n$(Resolve-ADTErrorRecord -ErrorRecord $_)"
Write-ADTLogEntry -Message $mainErrorMessage -Severity 3
## Error details hidden from the user by default. Show a simple dialog with full stack trace:
# Show-ADTDialogBox -Text $mainErrorMessage -Icon Stop -NoWait
## Or, a themed dialog with basic error message:
# Show-ADTInstallationPrompt -Message "$($adtSession.DeploymentType) failed at line $($_.InvocationInfo.ScriptLineNumber), char $($_.InvocationInfo.OffsetInLine):`n$($_.InvocationInfo.Line.Trim())`n`nMessage:`n$($_.Exception.Message)" -ButtonRightText OK -Icon Error -NoWait
Close-ADTSession -ExitCode 60001
}
finally
{
if (!(Get-Variable -Name mainErrorMessage -ErrorAction Ignore))
{
Initialize-ADTModule -ScriptDirectory $PSScriptRoot
if ($runAsActiveUser -and $script:namUninstalled)
{
Start-Sleep -Seconds 30 # allow time for user to see toast notification before restart prompt
Show-ADTInstallationRestartPrompt -CountdownSeconds 3600 -Title "Please restart your computer" -Subtitle "NAM has been uninstalled. Restart your computer to complete the process." -WindowLocation TopCenter
Start-Sleep -Seconds 3630
}
else
{
Show-ADTInstallationRestartPrompt -SilentRestart
}
}
}
I've tested this locally with psexec and i can't get it to restart hence why i was asking for guidance.
yes
I've tried Show-ADTInstallationRestartPrompt -SilentRestart -SilentCountdownSeconds 300 still the same. I want it to restart immediately afterwards during silent install hence why -SilentCountdownSeconds was not added.
using psexec i ran Invoke-appdeploytoolkit.exe -deploymenttype uninstall. The restart prompt works fine if a user is logged on. The logic in the finally block is working, they receive a prompt to restart their machine.
if no user is logged on silent install is triggered except the machine is not restarted.
this command in the else statement does not run Show-ADTInstallationRestartPrompt -SilentRestart i have tested with -SilentCountdownSeconds 5 and got the same result.