Merge pull request #494 from Romanitho/notif-optimized

Review system contect notification when ServiceUI is called.
pull/495/head
Romain 2023-11-21 15:23:39 +01:00 committed by GitHub
commit 9f632ede0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 20 deletions

View File

@ -12,31 +12,38 @@ Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName }
$null = cmd /c '' $null = cmd /c ''
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
#Check if running account is system or interactive logon
$Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem
#Log initialisation #Log initialisation
$LogFile = "$WorkingDir\logs\updates.log" $LogFile = "$WorkingDir\logs\updates.log"
#Check if running account is system or interactive logon
$Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem
#Check for current session ID (O = system without ServiceUI)
$Script:SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
#Check if running as system
if ($IsSystem) { if ($IsSystem) {
#Check if any connected user when running as system #Check if running with session ID 0
$explorerprocesses = @(Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue) if ($SessionID -eq 0) {
#Check if ServiceUI exists #Check if ServiceUI exists
$ServiceUI = Test-Path "$WorkingDir\ServiceUI.exe" $ServiceUI = Test-Path "$WorkingDir\ServiceUI.exe"
If ($explorerprocesses.Count -gt 0 -and $ServiceUI) { if ($ServiceUI) {
#User connected, Check for current session ID (O = system) #Check if any connected user
$SessionID = [System.Diagnostics.Process]::GetCurrentProcess().SessionId $explorerprocesses = @(Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
if ($SessionID -eq 0) { if ($explorerprocesses.Count -gt 0) {
#Rerun WAU in system context with ServiceUI #Rerun WAU in system context with ServiceUI
& $WorkingDir\ServiceUI.exe -process:explorer.exe $env:windir\System32\wscript.exe \`"$WorkingDir\Invisible.vbs\`" \`"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \`"\`"$WorkingDir\winget-upgrade.ps1\`"\`"\`" & $WorkingDir\ServiceUI.exe -process:explorer.exe $env:windir\System32\wscript.exe \`"$WorkingDir\Invisible.vbs\`" \`"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \`"\`"$WorkingDir\winget-upgrade.ps1\`"\`"\`"
Exit 0 Exit 0
} }
else { else {
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context with ServiceUI)" -IsHeader Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader
} }
} }
else { else {
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context)" -IsHeader Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context - No ServiceUI)" -IsHeader
}
}
else {
Write-ToLog -LogMsg "CHECK FOR APP UPDATES (System context - Connected user)" -IsHeader
} }
} }
else { else {

View File

@ -33,7 +33,7 @@ function Start-NotifTask {
$XMLimagepath = "$WorkingDir\icons\$MessageType.png" $XMLimagepath = "$WorkingDir\icons\$MessageType.png"
if (Test-Path $XMLimagepath) { if (Test-Path $XMLimagepath) {
# Creation of a image node # Creation of an image node
$XMLimage = $ToastTemplate.CreateElement("image") $XMLimage = $ToastTemplate.CreateElement("image")
$XMLbinding.AppendChild($XMLimage) | Out-Null $XMLbinding.AppendChild($XMLimage) | Out-Null
$XMLimageAtt1 = $ToastTemplate.CreateAttribute("placement") $XMLimageAtt1 = $ToastTemplate.CreateAttribute("placement")
@ -132,8 +132,8 @@ function Start-NotifTask {
$ToastTemplate.toast.SetAttribute("launch", $OnClickAction) | Out-Null $ToastTemplate.toast.SetAttribute("launch", $OnClickAction) | Out-Null
} }
#if not "Interactive" user, run as system #if not "Interactive" user, run Winget-AutoUpdate-Notify scheduled task
if ($IsSystem) { if ($SessionID -eq 0) {
#Save XML to File #Save XML to File
$ToastTemplateLocation = "$($WAUConfig.InstallLocation)\config\" $ToastTemplateLocation = "$($WAUConfig.InstallLocation)\config\"

View File

@ -26,7 +26,7 @@ function Write-ToLog {
#If header requested #If header requested
if ($IsHeader) { if ($IsHeader) {
$Log = " `n###########################################################`n# $LogMsg - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n###########################################################`n" $Log = " `n################################################################`n# $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern) - $LogMsg`n################################################################"
} }
else { else {
$Log = "$(Get-Date -UFormat "%T") - $LogMsg" $Log = "$(Get-Date -UFormat "%T") - $LogMsg"