Advertisement
dasirrine

Syncro TASK - Restart Windows if updates are pending

Sep 13th, 2024 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.90 KB | Source Code | 0 0
  1. Import-Module $env:SyncroModule -DisableNameChecking -ErrorAction Stop
  2.  
  3. <#
  4. Syncro TASK - Restart Windows if updates are pending.ps1
  5. v1.1 2024-09-13 by David Sirrine @ CNS  https://cnsroanoke.com
  6.  
  7. FUNCTION.
  8.     Syncro script to check for pending Windows updates using platform variable,
  9.     notify all logged in users, log the reason for the reboot to activity feed
  10.     of the Asset, then reboot the system after 60 seconds.
  11.  
  12. PURPOSE.
  13.     Broadcast messages can only be used when scripts are RunAs "Logged In User",
  14.     but not all users have permissions to run the "shutdown" command.
  15.  
  16. PREREQUISITES.
  17.     Relies on Syncro's platform variable {{asset_pending_reboot}} ;
  18.     Fails without this variable.
  19.  
  20. USAGE.
  21.     Run scheduled or realtime in Syncro. No parameters needed in this version.
  22.    
  23. PROPOSED REVISIONS.
  24.     1. integrate more extensive reboot checks to supplement platform variable,
  25.        e.g. https://github.com/adbertram/Random-PowerShell-Work/blob/master/Random%20Stuff/Test-PendingReboot.ps1
  26.     2. allow users to "snooze" the reboot a set amount of time and/or a certain number of times.
  27.     3. add option to schedule outside of "office hours".
  28. #>
  29.  
  30. #source of function: https://www.reddit.com/r/syncro/comments/kyqt91/broadcast_message_when_running_script_as_system/
  31. function MessageAsSystem ([string]$Title,[string]$Message,[int]$sDelay = 15) {
  32.  
  33.     $when = (Get-Date).AddSeconds($sDelay)
  34.     $argument = "--broadcast-message `"$Message`" --broadcast-title `"$Title`""
  35.  
  36.     # find logged-in users via running explorer.exe processes
  37.     $explorerProcesses = Get-CimInstance -ClassName Win32_Process -Filter "Name='explorer.exe'" -ErrorAction SilentlyContinue
  38.     $dUN = @($explorerProcesses | ForEach-Object { Invoke-CimMethod -InputObject $_ -MethodName GetOwner })
  39.     if ($dUN.Count -ne 0) {
  40.         # de-duplicate users in case of multiple explorer.exe processes
  41.         $LoggedInUsers = $dUN | Select-Object -Property Domain, User -Unique
  42.         # create scheduled task to notify each user
  43.         Foreach ($cUN in $LoggedInUsers) {
  44.             $username = ($cUN.Domain)+"\"+($cUN.User)
  45.             $thisuser = $cUN.User
  46.             Write-Host "   Scheduling task to notify user $username"
  47.             Register-ScheduledTask -TaskName "BroadcastMessage-$thisuser"  -User $username -InputObject (
  48.               (
  49.                 New-ScheduledTask -Action (
  50.                   New-ScheduledTaskAction -Execute "$env:ProgramFiles\RepairTech\Syncro\Syncro.App.Runner.exe" -Argument $argument
  51.                 ) -Trigger (
  52.                   New-ScheduledTaskTrigger -Once -At ($when.TimeOfDay.ToString("hh\:mm\:ss")) # As a "TimeOfDay" to get 24Hr format
  53.                 ) -Settings (
  54.                   New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter 00:00:05 # Delete five seconds after trigger expires
  55.                 ) -Principal (
  56.                   New-ScheduledTaskPrincipal -UserId $username
  57.                 )
  58.               ) | %{ $_.Triggers[0].EndBoundary = $when.AddMinutes(5).ToString('s') ; $_ } # Run through a pipe to set the end boundary of the trigger
  59.             )
  60.         }
  61.         #wait for notification tasks to run
  62.         Start-Sleep -Seconds ($sDelay)
  63.  
  64.     } else {
  65.         Write-Host "   Interactive user(s) not detected; skipping broadcast message"
  66.     }
  67. }
  68.  
  69. if ($RebootPending -eq "true") {
  70.     Write-Host "Reboot Pending"
  71.  
  72.     #messages for broadcast and logging
  73.     $title = "Updates Notification"
  74.     $user_message = "Your system needs to reboot to install important updates. Windows will restart in one (1) minute."
  75.     $system_message = "Rebooting Windows via script for pending updates"
  76.  
  77.     Write-Host "Notifying any logged in users of reboot"
  78.     MessageAsSystem -Message $user_message -Title $title -ErrorAction SilentlyContinue
  79.  
  80.     Write-Host "Logging an item on the Assets's Activity feed"
  81.     Log-Activity -Message $system_message -EventName $title -ErrorAction SilentlyContinue
  82.    
  83.     Write-Host "Rebooting in 60 seconds"
  84.     shutdown /g /f /t 60
  85.  
  86. } elseif ($RebootPending -eq "false") {
  87.     Write-Host "No reboot required"
  88.  
  89. } else {
  90.     Write-Host "Reboot status was not found; check for platform variable in script configuration."
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement