run scripts completely silent using one universal vbs

This commit is contained in:
ondrejs4 2021-01-22 10:04:03 +01:00
parent 9025d8618b
commit 49888017a6

View file

@ -82,18 +82,18 @@
** As well as added support for dynamic deadline retrieval for software updates **
** Stuff has been rewritten to suit my understanding and thoughts of the script **
2.0.0 - Huge changes to how this script handles custom protocols
Added Support for Custom Actions/Protocols within the script under user context removing the need for that to be run under SYSTEM/ADMIN
- <Option Name="Action" Value="ToastRunUpdateID:" />
- <Option Name="Action" Value="ToastRunPackageID:" />
- <Option Name="Action" Value="ToastRunApplicationID:" />
- <Option Name="Action" Value="ToastReboot:" />
Added Support to dynamically create Custom Action Scripts to support Custom Protocols
Added Support for Software (Feature) Updates : Searches for an update and will store in variable
Added new XML Types for Software Updates:
- <Option Name="RunUpdateID" Enabled="True" Value="3012973" />
- <Option Name="RunUpdateTitle" Enabled="True" Value="Version 1909" />
Added support for getting deadline date/time dynamically for software updates
2.0.0 - Huge changes to how this script handles custom protocols
Added Support for Custom Actions/Protocols within the script under user context removing the need for that to be run under SYSTEM/ADMIN
- <Option Name="Action" Value="ToastRunUpdateID:" />
- <Option Name="Action" Value="ToastRunPackageID:" />
- <Option Name="Action" Value="ToastRunApplicationID:" />
- <Option Name="Action" Value="ToastReboot:" />
Added Support to dynamically create Custom Action Scripts to support Custom Protocols
Added Support for Software (Feature) Updates : Searches for an update and will store in variable
Added new XML Types for Software Updates:
- <Option Name="RunUpdateID" Enabled="True" Value="3012973" />
- <Option Name="RunUpdateTitle" Enabled="True" Value="Version 1909" />
Added support for getting deadline date/time dynamically for software updates
- Configure DynamicDeadline with the UpdateID
2.0.1 - Updated custom action scripts!
@ -106,7 +106,7 @@
- If newer version is available from the script, new custom action scripts will be created
- This allows me to make sure the relevant scripts are in place in case I change something along the way
- Modified script output of custom script for RunPackageID to pick up Program ID dynamically
Added support for getting deadline date/time dynamically for applications
Added support for getting deadline date/time dynamically for applications
- Configure DynamicDeadline with the Application ID
2.0.2 - Fixed an error in the custom protocols
@ -267,21 +267,21 @@ function Get-GivenName() {
if (Get-Service -Name ccmexec -ErrorAction SilentlyContinue) {
Write-Log -Message "Looking for logged on user's SID in WMI with CCM client"
$LoggedOnSID = Get-CimInstance -Namespace ROOT\CCM -Class CCM_UserLogonEvents -Filter "LogoffTime=null" | Select -ExpandProperty UserSID
if ($LoggedOnSID.GetType().IsArray) {
Write-Log -Message "Multiple SID's found logged on. Skipping"
$GivenName = $null
if ($LoggedOnSID.GetType().IsArray) {
Write-Log -Message "Multiple SID's found logged on. Skipping"
$GivenName = $null
}
else {
$RegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData"
$DisplayName = (Get-ChildItem -Path $RegKey | Where-Object {$_.GetValue("LoggedOnUserSID") -eq $LoggedOnSID} | Select-Object -First 1).GetValue("LoggedOnDisplayName")
if ($DisplayName) {
$GivenName = $DisplayName.Split()[0].Trim()
Write-Log -Message "Given name found matching logged on user SID: $GivenName"
$GivenName
}
else {
$GivenName = $null
}
else {
$RegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData"
$DisplayName = (Get-ChildItem -Path $RegKey | Where-Object {$_.GetValue("LoggedOnUserSID") -eq $LoggedOnSID} | Select-Object -First 1).GetValue("LoggedOnDisplayName")
if ($DisplayName) {
$GivenName = $DisplayName.Split()[0].Trim()
Write-Log -Message "Given name found matching logged on user SID: $GivenName"
$GivenName
}
else {
$GivenName = $null
}
}
}
}
@ -788,7 +788,13 @@ 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"
# to be able to run vbs from Toast Notification (https://www.windows10forums.com/threads/custom-protocol-handler-does-not-work-in-ms-edge-in-win10.14698/)
# 0x210000 is join of these values:
# FTA_OpenIsSafe - 0x00010000
# FTA_SafeForElevation - 0x00200000
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762506(v=vs.85).aspx
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'EditFlags' -Value '0x210000' -PropertyType Dword -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = "wscript.exe $RegCommandPath\ToastRunScriptWithoutGUI.vbs" + " " + $RegCommandPath + '\' + "$($ActionType).cmd"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -803,7 +809,13 @@ 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"
# to be able to run vbs from Toast Notification (https://www.windows10forums.com/threads/custom-protocol-handler-does-not-work-in-ms-edge-in-win10.14698/)
# 0x210000 is join of these values:
# FTA_OpenIsSafe - 0x00010000
# FTA_SafeForElevation - 0x00200000
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762506(v=vs.85).aspx
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'EditFlags' -Value '0x210000' -PropertyType Dword -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = "wscript.exe $RegCommandPath\ToastRunScriptWithoutGUI.vbs" + " " + $RegCommandPath + '\' + "$($ActionType).ps1"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -818,7 +830,13 @@ 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"
# to be able to run vbs from Toast Notification (https://www.windows10forums.com/threads/custom-protocol-handler-does-not-work-in-ms-edge-in-win10.14698/)
# 0x210000 is join of these values:
# FTA_OpenIsSafe - 0x00010000
# FTA_SafeForElevation - 0x00200000
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762506(v=vs.85).aspx
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'EditFlags' -Value '0x210000' -PropertyType Dword -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = "wscript.exe $RegCommandPath\ToastRunScriptWithoutGUI.vbs" + " " + $RegCommandPath + '\' + "$($ActionType).ps1"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -833,7 +851,13 @@ 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"
# to be able to run vbs from Toast Notification (https://www.windows10forums.com/threads/custom-protocol-handler-does-not-work-in-ms-edge-in-win10.14698/)
# 0x210000 is join of these values:
# FTA_OpenIsSafe - 0x00010000
# FTA_SafeForElevation - 0x00200000
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762506(v=vs.85).aspx
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'EditFlags' -Value '0x210000' -PropertyType Dword -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = "wscript.exe $RegCommandPath\ToastRunScriptWithoutGUI.vbs" + " " + $RegCommandPath + '\' + "$($ActionType).ps1"
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
}
catch {
@ -842,6 +866,27 @@ function Write-CustomActionRegistry() {
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
#FIXME ToastRunPSBase64 I added in different branch, so just for you to know how to set its RegCommandValue
ToastRunPSBase64 {
# Build out registry for custom action for running PowerShell command encoded as Base64 via the action button
try {
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
# to be able to run vbs from Toast Notification (https://www.windows10forums.com/threads/custom-protocol-handler-does-not-work-in-ms-edge-in-win10.14698/)
# 0x210000 is join of these values:
# FTA_OpenIsSafe - 0x00010000
# FTA_SafeForElevation - 0x00200000
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762506(v=vs.85).aspx
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)" -Name 'EditFlags' -Value '0x210000' -PropertyType Dword -Force -ErrorAction SilentlyContinue | Out-Null
$RegCommandValue = "wscript.exe $RegCommandPath\ToastRunScriptWithoutGUI.vbs" + " " + $RegCommandPath + '\' + "$($ActionType).cmd `"%1`"" # %1 in quotes because it contains equal sign, which will be otherwise removed
New-ItemProperty -LiteralPath "HKCU:\Software\Classes\$($ActionType)\shell\open\command" -Name '(default)' -Value $RegCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
} catch {
Write-Log -Level Error "Failed to create the $ActionType custom protocol in HKCU\Software\Classes. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
}
}
@ -853,7 +898,7 @@ function Write-CustomActionScript() {
[CmdletBinding()]
param (
[Parameter(Position="0")]
[ValidateSet("ToastRunApplicationID","ToastRunPackageID","ToastRunUpdateID","ToastReboot")]
[ValidateSet("ToastRunApplicationID", "ToastRunPackageID", "ToastRunUpdateID", "ToastReboot", "ToastRunScriptWithoutGUI")]
[string] $Type,
[Parameter(Position="1")]
[String] $Path = $global:CustomScriptsPath
@ -862,34 +907,6 @@ function Write-CustomActionScript() {
switch ($Type) {
# Create custom scripts for running software updates via the action button
ToastRunUpdateID {
try {
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
try {
New-item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:CustomScriptsPath\ToastRunUpdateID.ps1`""
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$PS1FileName = $Type + '.ps1'
$PS1FilePath = $Path + '\' + $PS1FileName
@ -967,34 +984,6 @@ exit 0
# Previously this was hard coded to '*', making it work for task sequences only. Now also works for regular packages (only one program).
# Create custom scripts to run packages and task sequences directly from the action button
ToastRunPackageID {
try {
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
try {
New-item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:CustomScriptsPath\ToastRunPackageID.ps1`""
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$PS1FileName = $Type + '.ps1'
$PS1FilePath = $Path + '\' + $PS1FileName
@ -1038,35 +1027,6 @@ exit 0
}
# Create custom scripts to run applications directly from the action button
ToastRunApplicationID {
try {
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
try {
New-item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:CustomScriptsPath\ToastRunApplicationID.ps1`""
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$PS1FileName = $Type + '.ps1'
$PS1FilePath = $Path + '\' + $PS1FileName
@ -1113,13 +1073,109 @@ exit 0
# Do not run another type; break
Break
}
# Create custom scripts to run PowerShell command encoded as Base64 directly from the action button
ToastRunPSBase64 {
try {
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
try {
New-Item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
} catch {
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "
set passedArg=%~1
:: remove part before : from passed string
set base64=%passedArg:*:=%
powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -EncodedCommand %base64%"
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
} catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
} catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
# Do not run another type; break
Break
}
ToastRunScriptWithoutGUI {
try {
$VBSFileName = 'ToastRunScriptWithoutGUI.vbs'
try {
New-Item -Path $Path -Name $VBSFileName -Force -OutVariable PathInfo | Out-Null
} catch {
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
try {
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = @'
Set WshShell = WScript.CreateObject("WScript.Shell")
' regex to distinguish ps1 scripts
Set re = New RegExp
With re
.Pattern = "\.ps1$"
.IgnoreCase = True
.Global = False
End With
If Wscript.Arguments.Count < 1 Or Wscript.Arguments.Count > 2 Then
wscript.echo "ERROR, you have to enter one or two argument(s)! First has to be the path to cmd file to run and voluntarily second one as CMDs file argument"
ElseIf Wscript.Arguments.Count = 1 Then
If re.Test( WScript.Arguments(0) ) Then
' it is ps1 script
WshShell.Run "cmd /c powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File" & " " & """" & WScript.Arguments(0) & """", 0, True
Else
' it isn't ps1 script
WshShell.Run """" & WScript.Arguments(0) & """", 0, True
End If
ElseIf Wscript.Arguments.Count = 2 Then
'wscript.echo WScript.Arguments(0)
'wscript.echo WScript.Arguments(1)
If re.Test( WScript.Arguments(0) ) Then
' it is ps1 script
WshShell.Run "cmd /c powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File" & " " & """" & WScript.Arguments(0) & """" & " " & """" & WScript.Arguments(1) & """", 0, True
Else
' it isn't ps1 script
WshShell.Run """" & WScript.Arguments(0) & """" & """" & WScript.Arguments(1) & """", 0, True
End If
End If
Set WshShell = Nothing
'@
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
} catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
} catch {
Write-Log -Level Error "Failed to create the custom .cmd script for $Type. Action button might not work"
$ErrorMessage = $_.Exception.Message
Write-Log -Level Error -Message "Error message: $ErrorMessage"
}
}
}
}
######### GENERAL VARIABLES #########
# Global variables
# Setting global script version
$global:ScriptVersion = "2.1.0"
$global:ScriptVersion = "2.1.2"
# Setting executing directory
$global:ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
# Setting global custom action script location
@ -1629,6 +1685,7 @@ if ($CreateScriptsProtocolsEnabled -eq "True") {
Write-CustomActionScript -Type ToastRunApplicationID
Write-CustomActionScript -Type ToastRunPackageID
Write-CustomActionScript -Type ToastRunUpdateID
Write-CustomActionScript -Type ToastRunScriptWithoutGUI
New-ItemProperty -Path $global:RegistryPath -Name $RegistryName -Value $global:ScriptVersion -PropertyType "String" -Force | Out-Null
}
catch {