Skip to content

Commit

Permalink
- Resolved an issue introduced in v3.9.0 where toast notifications wo…
Browse files Browse the repository at this point in the history
…uld not display when run in the system context (#733)

- Resolved an issue introduced in v3.9.0 where toast notifications would not display when run in the system context (#733)
  • Loading branch information
seanlillis committed Jan 19, 2023
1 parent dc2f44c commit d306ea7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**Version 3.9.1 [xx/01/2023]**
- Resolved an issue introduced in v3.9.0 where toast notifications would not display when run in the system context (#733)
- Added Finnish language translations to the UI components (#681)
- Added variable $IsMultiSessionOS (#697)
- Add missing dash before TypeName parameter used with New-Object in the in Set-ItemPermission function (#721)
Expand Down
75 changes: 58 additions & 17 deletions Toolkit/AppDeployToolkit/AppDeployToolkitMain.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10524,15 +10524,50 @@ https://psappdeploytoolkit.com
}
Else {
Write-Log -Message "Displaying toast notification with message [$BalloonTipText]." -Source ${CmdletName}

$toastAppId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null

## Gets the Template XML so we can manipulate the values
$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
[xml] $ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())
[xml] $ToastTemplate = @"

[scriptblock]$toastScriptBlock = {
Param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[String]$BalloonTipText,
[Parameter(Mandatory = $false, Position = 1)]
[ValidateNotNullorEmpty()]
[String]$BalloonTipTitle,
[Parameter(Mandatory = $false, Position = 4)]
[ValidateNotNullorEmpty()]
[String]$AppDeployLogoImage
)

# Check for required entries in registry for when using Powershell as application for the toast
# Register the AppID in the registry for use with the Action Center, if required
$regPathNotificationSettings = 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings'
$toastAppId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'

# Create the registry entries if they don't exist
If (-not (Test-Path -Path "$regPathNotificationSettings\$toastAppId") ) {
$null = New-Item -Path "$regPathNotificationSettings\$toastAppId" -Force
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD'
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'Enabled' -Value 1 -PropertyType 'DWORD'
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'SoundFile' -PropertyType 'STRING'
}
# Make sure the app used with the action center is enabled
If ((Get-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'ShowInActionCenter' -ErrorAction 'SilentlyContinue').ShowInActionCenter -ne '1') {
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD' -Force
}
If ((Get-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'Enabled' -ErrorAction 'SilentlyContinue').Enabled -ne '1') {
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'Enabled' -Value 1 -PropertyType 'DWORD' -Force
}
If (!(Get-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'SoundFile' -ErrorAction 'SilentlyContinue')) {
$null = New-ItemProperty -Path "$regPathNotificationSettings\$toastAppId" -Name 'SoundFile' -PropertyType 'STRING' -Force
}

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null

## Gets the Template XML so we can manipulate the values
$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
[xml] $ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())
[xml] $ToastTemplate = @"
<toast launch="app-defined-string">
<visual>
<binding template="ToastImageAndText02">
Expand All @@ -10544,18 +10579,24 @@ https://psappdeploytoolkit.com
</toast>
"@

$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($ToastTemplate.OuterXml)

$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($ToastTemplate.OuterXml)

$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($toastAppId)
$notifier.Show($toastXml)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($toastAppId)
$notifier.Show($toastXml)

Start-Sleep -Seconds 10 #so Icon is shown properly in Toast
Start-Sleep 5 #so Icon is shown properly in Toast
}

## Invoke a separate PowerShell process as the current user passing the script block as a command and associated parameters to display the toast notification in the user context
Try {
$executeToastAsUserScript = "$configToolkitTempPath\$($appDeployToolkitName)-ToastNotification.ps1"
Set-Content -Path $executeToastAsUserScript -Value $toastScriptBlock -Force
Execute-ProcessAsUser -Path "$PSHOME\powershell.exe" -Parameters "-ExecutionPolicy Bypass -NoProfile -NoLogo -WindowStyle Hidden -Command & { & `"$executeToastAsUserScript `'$BalloonTipText`' `'$BalloonTipTitle`' `'$AppDeployLogoImage`'`"; Exit `$LastExitCode }" -Wait
}
Catch {
}
}


}
End {
Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer
Expand Down

0 comments on commit d306ea7

Please sign in to comment.