This commit is contained in:
Ondrej Sebela 2021-01-19 12:20:44 +01:00 committed by GitHub
commit 17c0c126e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 196 additions and 197 deletions

View file

@ -136,7 +136,7 @@ param(
[string]$Config
)
######### FUNCTIONS #########
#region ######## FUNCTIONS #########
# Create Write-Log function
function Write-Log() {
@ -438,10 +438,10 @@ function Get-DynamicDeadline() {
$UpdateID = Get-CMUpdate
}
# Getting application information from WMI
elseif ($RunApplicationIDEnabled -eq "True") {
elseif ($RunApplicationIDValue) {
Write-Log -Message "RunApplicationIDEnabled is True. Trying to get deadline information based on application id"
try {
$ApplicationID = Get-CimInstance -Namespace root\ccm\clientsdk -Query "SELECT * FROM CCM_Application where ID = '$DynDeadlineValue'"
$ApplicationID = Get-CimInstance -Namespace root\ccm\clientsdk -Query "SELECT * FROM CCM_Application where ID = '$RunApplicationIDValue'"
}
catch {
Write-Log -Level Error -Message "Failed to get Application ID from WMI"
@ -614,21 +614,8 @@ function Write-PackageIDRegistry() {
}
}
# Create Write-ApplicationIDRegistry function
function Write-ApplicationIDRegistry() {
Write-Log -Message "Running Write-ApplicationIDRegistry function"
$RegistryPath = "HKCU:\SOFTWARE\ToastNotificationScript"
$RegistryName = "RunApplicationID"
# Making sure that the registry path being used exists
if (-NOT(Test-Path -Path $RegistryPath)) {
try {
New-Item -Path $RegistryPath -Force
}
catch {
Write-Log -Level Error -Message "Error. Could not create ToastNotificationScript registry path"
}
}
# If the ApplicationID specified in the config.xml is picked up
# Create Test-ApplicationID function for checking, that App with given ID is deployed to the client
function Test-ApplicationID() {
if ($RunApplicationIDValue) {
# If the ConfigMgr service exist
if (Get-Service -Name ccmexec -ErrorAction SilentlyContinue) {
@ -643,15 +630,6 @@ function Write-ApplicationIDRegistry() {
# If the ApplicationID is found in WMI with the ConfigMgr client, tattoo that ApplicationID into registry
if ($TestApplicationID) {
Write-Log -Message "ApplicationID: $RunApplicationIDValue was found in WMI as deployed to the client"
Write-Log -Message "Writing the ApplicationID to registry"
if ((Get-ItemProperty -Path $RegistryPath -Name $RegistryName -ErrorAction SilentlyContinue).$RegistryName -ne $RunApplicationIDValue) {
try {
New-ItemProperty -Path $RegistryPath -Name $RegistryName -Value $RunApplicationIDValue -PropertyType "String" -Force
}
catch {
Write-Log -Level Error -Message "Failed to write ApplicationID: $RunApplicationIDValue to registry"
}
}
}
else {
Write-Log -Level Error -Message "ApplicationID: $RunApplicationIDValue was not found in WMI as deployed to the client. Please check the config.xml or deployment in ConfigMgr"
@ -788,7 +766,7 @@ function Write-CustomActionRegistry() {
New-Item "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name '(default)' -Value "URL:$($ActionType) Protocol" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = $RegCommandPath + '\' + "$($ActionType).cmd"
$RegCommandValue = $RegCommandPath + '\' + "$($ActionType).cmd %1"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -833,7 +811,7 @@ function Write-CustomActionRegistry() {
New-Item "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name '(default)' -Value "URL:$($ActionType) Protocol" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = $RegCommandPath + '\' + "$($ActionType).cmd"
$RegCommandValue = $RegCommandPath + '\' + "$($ActionType).cmd %1"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -1050,7 +1028,7 @@ exit 0
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:CustomScriptsPath\ToastRunApplicationID.ps1`""
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:CustomScriptsPath\ToastRunApplicationID.ps1`" %1"
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
@ -1080,19 +1058,25 @@ exit 0
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = @'
$RegistryPath = "HKCU:\SOFTWARE\ToastNotificationScript"
$ApplicationID = (Get-ItemProperty -Path $RegistryPath -Name "RunApplicationID").RunApplicationID
$TestApplicationID = Get-CimInstance -ClassName CCM_Application -Namespace ROOT\ccm\ClientSDK | Where-Object {$_.Id -eq $ApplicationID}
$ApplicationID = $args[0]
if (!$ApplicationID) { throw "ApplicationID wasn't specified" }
$ApplicationID = ($ApplicationID -replace '^[^:]*:').trim()
if ($ApplicationID -notmatch "^ScopeId_.+/Application_.+") { throw "ApplicationID ($ApplicationID) isn't in correct format (regex `"^ScopeId_.+/Application_.+`"" }
$TestApplicationID = Get-CimInstance -ClassName CCM_Application -Namespace ROOT\ccm\ClientSDK | Where-Object { $_.Id -eq $ApplicationID }
$AppArguments = @{
Id = $TestApplicationID.Id
IsMachineTarget = $TestApplicationID.IsMachineTarget
Revision = $TestApplicationID.Revision
}
if (-NOT[string]::IsNullOrEmpty($TestApplicationID)) {
if ($TestApplicationID.InstallState -eq "NotInstalled") { Invoke-CimMethod -Namespace "ROOT\ccm\clientSDK" -ClassName CCM_Application -MethodName Install -Arguments $AppArguments }
elseif ($TestApplicationID.InstallState -eq "Installed") { Invoke-CimMethod -Namespace "ROOT\ccm\clientSDK" -ClassName CCM_Application -MethodName Repair -Arguments $AppArguments }
if (Test-Path -Path "$env:windir\CCM\ClientUX\SCClient.exe") { Start-Process -FilePath "$env:windir\CCM\ClientUX\SCClient.exe" -ArgumentList "SoftwareCenter:Page=InstallationStatus" -WindowStyle Maximized }
}
exit 0
'@
if (-NOT[string]::IsNullOrEmpty($Script)) {
@ -1116,7 +1100,9 @@ exit 0
}
}
######### GENERAL VARIABLES #########
#endregion ######## FUNCTIONS #########
#region ######## GENERAL VARIABLES #########
# Global variables
# Setting global script version
$global:ScriptVersion = "2.1.0"
@ -1274,8 +1260,6 @@ if(-NOT[string]::IsNullOrEmpty($Xml)) {
$CreateScriptsProtocolsEnabled = $Xml.Configuration.Option | Where-Object {$_.Name -like 'CreateScriptsAndProtocols'} | Select-Object -ExpandProperty 'Enabled'
$RunPackageIDEnabled = $Xml.Configuration.Option | Where-Object {$_.Name -like 'RunPackageID'} | Select-Object -ExpandProperty 'Enabled'
$RunPackageIDValue = $Xml.Configuration.Option | Where-Object {$_.Name -like 'RunPackageID'} | Select-Object -ExpandProperty 'Value'
$RunApplicationIDEnabled = $Xml.Configuration.Option | Where-Object {$_.Name -like 'RunApplicationID'} | Select-Object -ExpandProperty 'Enabled'
$RunApplicationIDValue = $Xml.Configuration.Option | Where-Object {$_.Name -like 'RunApplicationID'} | Select-Object -ExpandProperty 'Value'
# ConfigMgr Software Updates
# Added in version 2.0.0
$RunUpdateIDEnabled = $Xml.Configuration.Option | Where-Object {$_.Name -like 'RunUpdateID'} | Select-Object -ExpandProperty 'Enabled'
@ -1359,6 +1343,38 @@ if(-NOT[string]::IsNullOrEmpty($Xml)) {
}
}
#endregion ####### GENERAL VARIABLES #########
#region ####### CHECKS #########
if ($ActionButton1Content -match "^ToastRunApplicationID:\s*$") {
Write-Log -Level Error -Message "Error. Incomplete Value in the $Config file Action1 tag"
Write-Log -Level Error -Message "Error. You have to specify also the Application share link in format: ToastRunApplicationID:ScopeId_XXX/Application_YYY"
Write-Log -Level Error -Message "Error. Application Share link can be found in Software Center by clicking on share icon on top right of the screen"
Exit 1
}
if ($ActionButton2Content -match "^ToastRunApplicationID:\s*$") {
Write-Log -Level Error -Message "Error. Incomplete Value in the $Config file Action2 tag"
Write-Log -Level Error -Message "Error. You have to specify also the Application share link in format: ToastRunApplicationID:ScopeId_XXX/Application_YYY"
Write-Log -Level Error -Message "Error. Application Share link can be found in Software Center by clicking on share icon on top right of the screen"
Exit 1
}
# Check if given Application ID is valid
if ($ActionButton1Content -match "^ToastRunApplicationID:ScopeId_.+/Application_.+") {
$RunApplicationIDValue = ($ActionButton1Content -replace '^[^:]*:').trim()
Test-ApplicationID
}
if ($ActionButton2Content -match "^ToastRunApplicationID:ScopeId_.+/Application_.+") {
$RunApplicationIDValue = ($ActionButton2Content -replace '^[^:]*:').trim()
Test-ApplicationID
}
# because of dynamic deadline retrieval I allow just one installation
if ($ActionButton2Content -match "^ToastRunApplicationID:" -and $RunApplicationIDValue) {
Write-Log -Level Error -Message "Error. Multiple Application installations defined in the $Config file through the Action tags"
Write-Log -Level Error -Message "Error. Only one application can be installed, so modify one of Action options"
Exit 1
}
# Check if toast is enabled in config.xml
if ($ToastEnabled -ne "True") {
Write-Log -Message "Toast notification is not enabled. Please check $Config file"
@ -1456,18 +1472,6 @@ if (($DeadlineEnabled -eq "True") -AND ($DynDeadlineEnabled -eq "True")) {
Write-Log -Level Error -Message "You should only enable one of the deadline options. Check your config"
Exit 1
}
if (($RunApplicationIDEnabled -eq "True") -AND ($RunPackageIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "Error. You can't have RunApplicationIDEnabled set to True and RunPackageIDEnabled set to True at the same time"
Write-Log -Level Error -Message "You should only enable one of the options. Check your config"
Exit 1
}
if (($RunApplicationIDEnabled -eq "True") -AND ($RunUpdateIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "Error. You can't have RunApplicationIDEnabled set to True and RunUpdateIDEnabled set to True at the same time"
Write-Log -Level Error -Message "You should only enable one of the options. Check your config"
Exit 1
}
if (($RunUpdateIDEnabled -eq "True") -AND ($RunPackageIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "Error. You can't have RunUpdateIDEnabled set to True and RunPackageIDEnabled set to True at the same time"
@ -1513,24 +1517,6 @@ if (($Action -eq "ToastReboot:") -AND ($RunPackageIDEnabled -eq "True")) {
Write-Log -Level Error -Message "This seems like an unintended configuration. Check your config"
Exit 1
}
if (($Action -eq "ToastRunPackageID:") -AND ($RunApplicationIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "You are using the toast notification with RunApplicationIDEnabled set to $RunApplicationIDEnabled, but the action button is set to $Action"
Write-Log -Level Error -Message "This seems like an unintended configuration. Check your config"
Exit 1
}
if (($Action -eq "ToastRunUpdateID:") -AND ($RunApplicationIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "You are using the toast notification with RunApplicationIDEnabled set to $RunApplicationIDEnabled, but the action button is set to $Action"
Write-Log -Level Error -Message "This seems like an unintended configuration. Check your config"
Exit 1
}
if (($Action -eq "ToastReboot:") -AND ($RunApplicationIDEnabled -eq "True")) {
Write-Log -Level Error -Message "Error. Conflicting selection in the $Config file"
Write-Log -Level Error -Message "You are using the toast notification with RunApplicationIDEnabled set to $RunApplicationIDEnabled, but the action button is set to $Action"
Write-Log -Level Error -Message "This seems like an unintended configuration. Check your config"
Exit 1
}
# New checks for conflicting selections. Trying to prevent combinations which will make the toast render without buttons
# Added in version 2.1.0
if (($ActionButton1Enabled -ne "True") -AND ($ActionButton2Enabled -eq "True")){
@ -1563,6 +1549,21 @@ if (($SnoozeButtonEnabled -eq "True") -AND ($ADPasswordExpirationTextEnabled -eq
Write-Log -Level Error -Message "That will result in too much text and the toast notification will render without buttons. Check your config"
Exit 1
}
# New checks for value format
if ($Action1 -match "ToastRunApplicationID:" -and (($Action1.trim() -match " ") -or $Action1 -notmatch "ToastRunApplicationID:ScopeId_.+/Application_.+")) {
Write-Log -Level Error -Message "Value of Action1 XML config tag isn't in correct format"
Write-Log -Level Error -Message "It should be like: ToastRunApplicationID:ScopeId_5197DD71-3116-4878-A26C-EA6155A28E9B/Application_31e422a6-3905-4f83-b9aa-cf1635648e72"
Write-Log -Level Error -Message "Value can be found in Software Center by clicking on share icon of application to install"
Exit 1
}
if ($Action2 -match "ToastRunApplicationID:" -and (($Action2.trim() -match " ") -or $Action2 -notmatch "ToastRunApplicationID:ScopeId_.+/Application_.+")) {
Write-Log -Level Error -Message "Value of Action2 XML config tag isn't in correct format"
Write-Log -Level Error -Message "It should be like: ToastRunApplicationID:ScopeId_5197DD71-3116-4878-A26C-EA6155A28E9B/Application_31e422a6-3905-4f83-b9aa-cf1635648e72"
Write-Log -Level Error -Message "Value can be found in Software Center by clicking on share icon of application to install"
Exit 1
}
#endregion ######## CHECKS #########
# Downloading images into user's temp folder if images are hosted online
if (($LogoImageFileName.StartsWith("https://")) -OR ($LogoImageFileName.StartsWith("http://"))) {
@ -1649,9 +1650,8 @@ if ($RunUpdateIDEnabled -eq "True") {
}
# Running RunApplicationID function
if ($RunApplicationIDEnabled -eq "True") {
if ($RunApplicationIDValue) {
Write-Log -Message "RunApplicationID set to True. Will allow execution of ApplicationID directly from the toast action button"
Write-ApplicationIDRegistry
}
# Running RunPackageID function

View file

@ -12,7 +12,6 @@
<Option Name="ADPasswordExpirationText" Enabled="False" /> <!-- Adds an additional group to the toast with text -->
<Option Name="ADPasswordExpirationDays" Value="90" /> <!-- How many days in advance shall the toast start reminding the users -->
<Option Name="RunPackageID" Enabled="False" Value="KR100907" /> <!-- Will enable the toast to run any ConfigMgr PackageID through a custom protocol -->
<Option Name="RunApplicationID" Enabled="False" Value="ScopeId_A9117680-D054-482B-BC97-532E6CBD0E6B/Application_fd55f35c-4e34-4490-a3ec-ee0b79233ec6" /> <!-- Will enable the toast to run any ConfigMgr ApplicationID through a custom protocol -->
<Option Name="RunUpdateID" Enabled="False" Value="4561600" /> <!-- Will enable the toast to run any ConfigMgr Update ID through a custom protocol. Configure the value to the relevant KB-article ID -->
<Option Name="RunUpdateTitle" Enabled="False" Value="" /> <!-- Will enable the toast to run any ConfigMgr Update Name through a custom protocol -->
<Option Name="Deadline" Enabled="False" Value="30-09-2019 08:00" /> <!-- Adds an additional group to the toast with text about the deadline of the OSUpgrade -->
@ -28,7 +27,7 @@
<Option Name="DismissButton" Enabled="False" /> <!-- Enables or disables the dismiss button. -->
<Option Name="SnoozeButton" Enabled="False" /> <!-- Enabling this option will always enable action button and dismiss button -->
<Option Name="Scenario" Type="alarm" /> <!-- Possible values are: reminder | short | long -->
<Option Name="Action1" Value="ToastReboot:" /> <!-- Action taken when using the Action button. Can be any protocol in Windows -->
<Option Name="Action1" Value="ToastRunApplicationID:ScopeId_5197DD71-3116-4878-A26C-EA6155A28E9B/Application_91b60d2e-720e-46b4-8da9-5b56f9ff0177" /> <!-- Action taken when using the Action button. Can be any protocol in Windows -->
<Option Name="Action2" Value="https://imab.dk" /> <!-- Action taken when using the Action button. Can be any protocol in Windows -->
<Text Option="GreetGivenName" Enabled="True" /> <!-- Displays the toast with a personal greeting using the users given name retrieved from AD. Will try retrieval from WMI of no local AD -->
<Text Option="MultiLanguageSupport" Enabled="False" /> <!-- Enable support for multiple languages. If set to True, the toast notification will look for the users language culture within the config file -->