Update winget-upgrade.ps1

pull/3/head
Romain 2022-02-19 15:29:39 +01:00
parent 8ee8c5ba5a
commit 2d4ed7c565
1 changed files with 27 additions and 13 deletions

View File

@ -42,7 +42,7 @@ function Write-Log ($LogMsg,$LogColor = "White") {
#Echo log #Echo log
$Log | Write-host -ForegroundColor $LogColor $Log | Write-host -ForegroundColor $LogColor
#Write log to file #Write log to file
$Log | out-file -filepath $LogFile -Append $Log | Out-File -filepath $LogFile -Append
} }
function Start-NotifTask ($Title,$Message,$MessageType,$Balise) { function Start-NotifTask ($Title,$Message,$MessageType,$Balise) {
@ -61,22 +61,36 @@ function Start-NotifTask ($Title,$Message,$MessageType,$Balise) {
</toast> </toast>
"@ "@
#Save XML File #Check if running account is system or interactive logon
$currentPrincipal = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-4")
if ($currentPrincipal -eq $false){
#Save XML to File
$ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\" $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\"
if (!(Test-Path $ToastTemplateLocation)){ if (!(Test-Path $ToastTemplateLocation)){
New-Item -ItemType Directory -Force -Path $ToastTemplateLocation New-Item -ItemType Directory -Force -Path $ToastTemplateLocation
} }
$ToastTemplate.Save("$ToastTemplateLocation\notif.xml") $ToastTemplate.Save("$ToastTemplateLocation\notif.xml")
#Send Notification to user. First, check if script is run as admin (or system)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
#Run Notify scheduled task to notify conneted users #Run Notify scheduled task to notify conneted users
Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
} }
#else, run as user #else, run as connected user
else{ else{
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$WorkingDir\winget-notify.ps1`"" -NoNewWindow -Wait #Load Assemblies
[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
#Prepare XML
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
$ToastXml.LoadXml($ToastTemplate.OuterXml)
#Specify Launcher App ID
$LauncherID = "Windows.SystemToast.Winget.Notification"
#Prepare and Create Toast
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXml)
$ToastMessage.Tag = $ToastTemplate.toast.tag
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)
} }
#Wait for notification to display #Wait for notification to display