Version 1.2.14 - 14/01/21
Version 1.2.14 - 14/01/21 -Fixed logic to return logged on DisplayName - Thanks @MMelkersen -Changed the way we retrieve the SID for the current user variable $LoggedOnUserSID -Added Event Title, Description and Source Path to the Scheduled Task that is created to pop the User Toast -Fixed an issue where Snooze was not being passed from the Scheduled Task -Fixed an issue with XMLSource full path not being returned correctly from Scheduled Task
This commit is contained in:
parent
7ba75ac8e0
commit
353b0da6f8
1 changed files with 103 additions and 104 deletions
159
Toast_Notify.ps1
159
Toast_Notify.ps1
|
|
@ -5,6 +5,13 @@ Created by: Ben Whitmore
|
||||||
Filename: Toast_Notify.ps1
|
Filename: Toast_Notify.ps1
|
||||||
===========================================================================
|
===========================================================================
|
||||||
|
|
||||||
|
Version 1.2.14 - 14/01/21
|
||||||
|
-Fixed logic to return logged on DisplayName - Thanks @MMelkersen
|
||||||
|
-Changed the way we retrieve the SID for the current user variable $LoggedOnUserSID
|
||||||
|
-Added Event Title, Description and Source Path to the Scheduled Task that is created to pop the User Toast
|
||||||
|
-Fixed an issue where Snooze was not being passed from the Scheduled Task
|
||||||
|
-Fixed an issue with XMLSource full path not being returned correctly from Scheduled Task
|
||||||
|
|
||||||
Version 1.2.10 - 10/01/21
|
Version 1.2.10 - 10/01/21
|
||||||
-Removed XMLOtherSource Parameter
|
-Removed XMLOtherSource Parameter
|
||||||
-Cleaned up XML formatting which removed unnecessary duplication when the Snooze parameter was passed. Action ChildNodes are now appended to ToastTemplate XML.
|
-Cleaned up XML formatting which removed unnecessary duplication when the Snooze parameter was passed. Action ChildNodes are now appended to ToastTemplate XML.
|
||||||
|
|
@ -67,90 +74,41 @@ $CurrentDir = Split-Path $ScriptPath
|
||||||
|
|
||||||
#Get Logged On User to prepare Scheduled Task
|
#Get Logged On User to prepare Scheduled Task
|
||||||
$LoggedOnUserName = (Get-CimInstance -Namespace "root\cimv2" -ClassName Win32_ComputerSystem).Username
|
$LoggedOnUserName = (Get-CimInstance -Namespace "root\cimv2" -ClassName Win32_ComputerSystem).Username
|
||||||
$LoggedOnUserSID = (Get-CimInstance -Namespace "root\cimv2" -ClassName Win32_UserAccount | Where-Object { $_.Caption -eq $LoggedOnUserName }).SID
|
$LoggedOnUserSID = ([System.Security.Principal.NTAccount]($LoggedOnUserName)).Translate([System.Security.Principal.SecurityIdentifier]).Value
|
||||||
|
|
||||||
#Get list of User Profiles
|
|
||||||
$ProfilePath = Get-CimInstance -Namespace "root\cimv2" -ClassName "Win32_UserProfile"
|
|
||||||
|
|
||||||
# Get Profile Path for LoggedOnUser
|
# Get Profile Path for LoggedOnUser
|
||||||
Foreach ($Profile in $ProfilePath) {
|
Try {
|
||||||
Try {
|
|
||||||
If ($LoggedOnUserSID -eq $Profile.SID) {
|
|
||||||
|
|
||||||
#Set Toast Path to UserProfile Temp Directory
|
#Set Toast Path to UserProfile Temp Directory
|
||||||
$LoggedOnUserToastPath = (Join-Path $Profile.LocalPath "AppData\Local\Temp\$($ToastGuid)")
|
$LocalUserPath = (Get-CimInstance -Namespace "root\cimv2" -ClassName "Win32_UserProfile" | Where-Object { $_.SID -eq $LoggedOnUserSID }).LocalPath
|
||||||
}
|
$LoggedOnUserToastPath = (Join-Path $LocalUserPath "AppData\Local\Temp\$($ToastGuid)")
|
||||||
}
|
}
|
||||||
Catch {
|
Catch {
|
||||||
Write-Warning $_.Exception.Message
|
Write-Warning $_.Exception.Message
|
||||||
Write-Warning "Error resolving Logged on User SID to a valid Profile Path"
|
Write-Warning "Error resolving Logged on User SID to a valid Profile Path"
|
||||||
|
|
||||||
#Set Toast Path to C:\Windows\Temp if user profile path cannot be resolved
|
#Set Toast Path to C:\Windows\Temp if user profile path cannot be resolved
|
||||||
$LoggedOnUserToastPath = (Join-Path $ENV:Windir "Temp\$($ToastGuid)")
|
$LoggedOnUserToastPath = (Join-Path $ENV:Windir "Temp\$($ToastGuid)")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#Create TEMP folder to stage Toast Notification Content in %TEMP% Folder
|
#Test if XML exists
|
||||||
Try {
|
if (!(Test-Path (Join-Path $CurrentDir $XMLSource))) {
|
||||||
New-Item $LoggedOnUserToastPath -ItemType Directory -ErrorAction Continue | Out-Null
|
|
||||||
Try {
|
|
||||||
$ToastFiles = Get-ChildItem $CurrentDir -Recurse
|
|
||||||
|
|
||||||
#Copy Toast Files to Toat TEMP folder
|
|
||||||
ForEach ($ToastFile in $ToastFiles) {
|
|
||||||
Copy-Item (Join-Path $CurrentDir $ToastFile) -Destination $LoggedOnUserToastPath -ErrorAction Continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Catch {
|
|
||||||
Write-Warning $_.Exception.Message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Catch {
|
|
||||||
Write-Warning $_.Exception.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
#Dont Create a Scheduled Task if the script is running in the context of the logged on user, only if SYSTEM fired the script i.e. Deployment from Intune/ConfigMgr
|
|
||||||
If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq "NT AUTHORITY\SYSTEM") {
|
|
||||||
|
|
||||||
#Set new Toast script to run from TEMP path
|
|
||||||
$New_ToastPath = Join-Path $LoggedOnUserToastPath "Toast_Notify.ps1"
|
|
||||||
|
|
||||||
#Created Scheduled Task to run as Logged on User
|
|
||||||
$Task_TimeToRun = (Get-Date).AddSeconds(30).ToString('s')
|
|
||||||
$Task_Expiry = (Get-Date).AddSeconds(120).ToString('s')
|
|
||||||
$Task_Action = New-ScheduledTaskAction -Execute "C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -File $New_ToastPath -ToastGUID $ToastGUID"
|
|
||||||
$Task_Trigger = New-ScheduledTaskTrigger -Once -At $Task_TimeToRun
|
|
||||||
$Task_Trigger.EndBoundary = $Task_Expiry
|
|
||||||
$Task_Principal = New-ScheduledTaskPrincipal -UserId $LoggedOnUserName -LogonType ServiceAccount
|
|
||||||
$Task_Settings = New-ScheduledTaskSettingsSet -Compatibility V1 -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 600)
|
|
||||||
$New_Task = New-ScheduledTask -Description "Toast_Notification_$($LoggedOnUserSID)_$($ToastGuid) Task for user notification" -Action $Task_Action -Principal $Task_Principal -Trigger $Task_Trigger -Settings $Task_Settings
|
|
||||||
Register-ScheduledTask -TaskName "Toast_Notification_$($LoggedOnUserSID)_$($ToastGuid)" -InputObject $New_Task
|
|
||||||
}
|
|
||||||
|
|
||||||
#Run the toast of the script is running in the context of the Logged On User
|
|
||||||
If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq $LoggedOnUserName) {
|
|
||||||
|
|
||||||
#Test if XML exists
|
|
||||||
if (!(Test-Path -Path $XMLSource)) {
|
|
||||||
throw "$XMLSource is invalid."
|
throw "$XMLSource is invalid."
|
||||||
}
|
}
|
||||||
|
|
||||||
#Check XML is valid
|
#Check XML is valid
|
||||||
$XMLToast = New-Object System.Xml.XmlDocument
|
$XMLToast = New-Object System.Xml.XmlDocument
|
||||||
try {
|
try {
|
||||||
$XMLToast.Load((Get-ChildItem -Path $XMLSource).FullName)
|
$XMLToast.Load((Get-ChildItem -Path (Join-Path $CurrentDir $XMLSource)).FullName)
|
||||||
$XMLValid = $True
|
$XMLValid = $True
|
||||||
}
|
}
|
||||||
catch [System.Xml.XmlException] {
|
catch [System.Xml.XmlException] {
|
||||||
Write-Verbose "$XMLSource : $($_.toString())"
|
Write-Verbose "$XMLSource : $($_.toString())"
|
||||||
$XMLValid = $False
|
$XMLValid = $False
|
||||||
}
|
}
|
||||||
|
|
||||||
#Continue if XML is valid
|
#Continue if XML is valid
|
||||||
If ($XMLValid -eq $True) {
|
If ($XMLValid -eq $True) {
|
||||||
|
|
||||||
#Read XML Nodes
|
|
||||||
[XML]$Toast = Get-Content $XMLSource
|
|
||||||
|
|
||||||
#Create Toast Variables
|
#Create Toast Variables
|
||||||
$ToastTitle = $XMLToast.ToastContent.ToastTitle
|
$ToastTitle = $XMLToast.ToastContent.ToastTitle
|
||||||
|
|
@ -164,9 +122,6 @@ If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq $Logged
|
||||||
#ToastDuration: Short = 7s, Long = 25s
|
#ToastDuration: Short = 7s, Long = 25s
|
||||||
$ToastDuration = "long"
|
$ToastDuration = "long"
|
||||||
|
|
||||||
#Toast Time Format
|
|
||||||
$Time = Get-Date -Format HH:mm
|
|
||||||
|
|
||||||
#Images
|
#Images
|
||||||
$BadgeImage = "file:///$CurrentDir/badgeimage.jpg"
|
$BadgeImage = "file:///$CurrentDir/badgeimage.jpg"
|
||||||
$HeroImage = "file:///$CurrentDir/heroimage.jpg"
|
$HeroImage = "file:///$CurrentDir/heroimage.jpg"
|
||||||
|
|
@ -176,23 +131,67 @@ If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq $Logged
|
||||||
#$LauncherID = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe"
|
#$LauncherID = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe"
|
||||||
$Launcherid = "MSEdge"
|
$Launcherid = "MSEdge"
|
||||||
|
|
||||||
#Get last(current) logged on user
|
#Dont Create a Scheduled Task if the script is running in the context of the logged on user, only if SYSTEM fired the script i.e. Deployment from Intune/ConfigMgr
|
||||||
$LoggedOnUserPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnDisplayName"
|
If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq "NT AUTHORITY\SYSTEM") {
|
||||||
If (!(Test-Path $LoggedOnUserPath)) {
|
|
||||||
|
#Prepare to stage Toast Notification Content in %TEMP% Folder
|
||||||
Try {
|
Try {
|
||||||
$Firstname = $Null
|
|
||||||
|
#Create TEMP folder to stage Toast Notification Content in %TEMP% Folder
|
||||||
|
New-Item $LoggedOnUserToastPath -ItemType Directory -Force -ErrorAction Continue | Out-Null
|
||||||
|
$ToastFiles = Get-ChildItem $CurrentDir -Recurse
|
||||||
|
|
||||||
|
#Copy Toast Files to Toat TEMP folder
|
||||||
|
ForEach ($ToastFile in $ToastFiles) {
|
||||||
|
Copy-Item (Join-Path $CurrentDir $ToastFile) -Destination $LoggedOnUserToastPath -ErrorAction Continue
|
||||||
}
|
}
|
||||||
Catch [System.Management.Automation.ItemNotFoundException] {
|
|
||||||
Write-Warning "$RegistryKey was not found."
|
|
||||||
}
|
}
|
||||||
Catch {
|
Catch {
|
||||||
Write-Warning "Error $($error[0].Exception)."
|
Write-Warning $_.Exception.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
#Set new Toast script to run from TEMP path
|
||||||
|
$New_ToastPath = Join-Path $LoggedOnUserToastPath "Toast_Notify.ps1"
|
||||||
|
|
||||||
|
#Created Scheduled Task to run as Logged on User
|
||||||
|
$Task_TimeToRun = (Get-Date).AddSeconds(30).ToString('s')
|
||||||
|
$Task_Expiry = (Get-Date).AddSeconds(120).ToString('s')
|
||||||
|
If ($Snooze) {
|
||||||
|
$Task_Action = New-ScheduledTaskAction -Execute "C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -File ""$New_ToastPath"" -ToastGUID ""$ToastGUID"" -Snooze"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Task_Action = New-ScheduledTaskAction -Execute "C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -File ""$New_ToastPath"" -ToastGUID ""$ToastGUID"""
|
||||||
|
}
|
||||||
|
$Task_Trigger = New-ScheduledTaskTrigger -Once -At $Task_TimeToRun
|
||||||
|
$Task_Trigger.EndBoundary = $Task_Expiry
|
||||||
|
$Task_Principal = New-ScheduledTaskPrincipal -UserId $LoggedOnUserName -LogonType ServiceAccount
|
||||||
|
$Task_Settings = New-ScheduledTaskSettingsSet -Compatibility V1 -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 600)
|
||||||
|
$New_Task = New-ScheduledTask -Description "Toast_Notification_$($LoggedOnUserSID)_$($ToastGuid) Task for user notification. Title: $($EventTitle) :: Event:$($EventText) :: Source Path: $($LoggedOnUserToastPath) " -Action $Task_Action -Principal $Task_Principal -Trigger $Task_Trigger -Settings $Task_Settings
|
||||||
|
Register-ScheduledTask -TaskName "Toast_Notification_$($LoggedOnUserSID)_$($ToastGuid)" -InputObject $New_Task
|
||||||
|
}
|
||||||
|
|
||||||
|
#Run the toast of the script is running in the context of the Logged On User
|
||||||
|
If (([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name -eq $LoggedOnUserName) {
|
||||||
|
|
||||||
|
#Get logged on user DisplayName
|
||||||
|
Try {
|
||||||
|
If (Get-Itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" -Name "LastLoggedOnDisplayName" -ErrorAction SilentlyContinue) {
|
||||||
|
$User = Get-Itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" -Name "LastLoggedOnDisplayName" | Select-Object -ExpandProperty LastLoggedOnDisplayName
|
||||||
|
If ($Null -eq $User) {
|
||||||
|
$Firstname = $Null
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$DisplayName = $User.Split(" ")
|
||||||
|
$Firstname = $DisplayName[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$User = Get-Itemproperty -Path $LoggedOnUserPath -Name "LastLoggedOnDisplayName" | Select-Object -ExpandProperty LastLoggedOnDisplayName
|
$Firstname = $Null
|
||||||
$DisplayName = $User.Split(" ")
|
}
|
||||||
$Firstname = $DisplayName[0]
|
}
|
||||||
|
Catch {
|
||||||
|
Write-Warning "Warning: Registry value for LastLoggedOnDisplayName could not be found: $($error[0].Exception)."
|
||||||
|
$Firstname = $Null
|
||||||
}
|
}
|
||||||
|
|
||||||
#Get Hour of Day and set Custom Hello
|
#Get Hour of Day and set Custom Hello
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue