| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Jon Hawkesworth (@jhawkesworth) <[email protected]> |
| 4 |
# Copyright: (c) 2017, Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 8 |
|
| 9 |
$ErrorActionPreference = "Stop" |
| 10 |
|
| 11 |
# version check |
| 12 |
$osversion = [Environment]::OSVersion |
| 13 |
$lowest_version = 10 |
| 14 |
if ($osversion.Version.Major -lt $lowest_version ) { |
| 15 |
$msg = "Sorry, this version of windows, $osversion, does not support Toast notifications. Toast notifications are available from version $lowest_version" |
| 16 |
Fail-Json -obj $result -message $msg |
| 17 |
} |
| 18 |
|
| 19 |
$stopwatch = [system.diagnostics.stopwatch]::startNew() |
| 20 |
$now = [DateTime]::Now |
| 21 |
$default_title = "Notification: " + $now.ToShortTimeString() |
| 22 |
|
| 23 |
$params = Parse-Args $args -supports_check_mode $true |
| 24 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 25 |
|
| 26 |
$expire_seconds = Get-AnsibleParam -obj $params -name "expire" -type "int" -default 45 |
| 27 |
$group = Get-AnsibleParam -obj $params -name "group" -type "str" -default "Powershell" |
| 28 |
$msg = Get-AnsibleParam -obj $params -name "msg" -type "str" -default "Hello world!" |
| 29 |
$popup = Get-AnsibleParam -obj $params -name "popup" -type "bool" -default $true |
| 30 |
$tag = Get-AnsibleParam -obj $params -name "tag" -type "str" -default "Ansible" |
| 31 |
$title = Get-AnsibleParam -obj $params -name "title" -type "str" -default $default_title |
| 32 |
|
| 33 |
$timespan = New-TimeSpan -Seconds $expire_seconds |
| 34 |
$expire_at = $now + $timespan |
| 35 |
$expire_at_utc = $($expire_at.ToUniversalTime() | Out-String).Trim() |
| 36 |
|
| 37 |
$result = @{ |
| 38 |
changed = $false |
| 39 |
expire_at = $expire_at.ToString() |
| 40 |
expire_at_utc = $expire_at_utc |
| 41 |
toast_sent = $false |
| 42 |
} |
| 43 |
|
| 44 |
# If no logged in users, there is no notifications service, |
| 45 |
# and no-one to read the message, so exit but do not fail |
| 46 |
# if there are no logged in users to notify. |
| 47 |
|
| 48 |
if ((Get-Process -Name explorer -ErrorAction SilentlyContinue).Count -gt 0) { |
| 49 |
|
| 50 |
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null |
| 51 |
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02) |
| 52 |
|
| 53 |
#Convert to .NET type for XML manipulation |
| 54 |
$toastXml = [xml] $template.GetXml() |
| 55 |
$toastXml.GetElementsByTagName("text").Item(0).AppendChild($toastXml.CreateTextNode($title)) > $null |
| 56 |
$toastXml.GetElementsByTagName("text").Item(1).AppendChild($toastXml.CreateTextNode($msg)) > $null |
| 57 |
|
| 58 |
#Convert back to WinRT type |
| 59 |
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument |
| 60 |
$xml.LoadXml($toastXml.OuterXml) |
| 61 |
|
| 62 |
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml) |
| 63 |
$toast.Tag = $tag |
| 64 |
$toast.Group = $group |
| 65 |
$toast.ExpirationTime = $expire_at |
| 66 |
$toast.SuppressPopup = -not $popup |
| 67 |
|
| 68 |
try { |
| 69 |
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Ansible') |
| 70 |
if (-not $check_mode) { |
| 71 |
$notifier.Show($toast) |
| 72 |
$result.toast_sent = $true |
| 73 |
Start-Sleep -Seconds $expire_seconds |
| 74 |
} |
| 75 |
} |
| 76 |
catch { |
| 77 |
$excep = $_ |
| 78 |
$result.exception = $excep.ScriptStackTrace |
| 79 |
Fail-Json -obj $result -message "Failed to create toast notifier: $($excep.Exception.Message)" |
| 80 |
} |
| 81 |
} |
| 82 |
else { |
| 83 |
$result.toast_sent = $false |
| 84 |
$result.no_toast_sent_reason = 'No logged in users to notify' |
| 85 |
} |
| 86 |
|
| 87 |
$endsend_at = Get-Date | Out-String |
| 88 |
$stopwatch.Stop() |
| 89 |
|
| 90 |
$result.time_taken = $stopwatch.Elapsed.TotalSeconds |
| 91 |
$result.sent_localtime = $endsend_at.Trim() |
| 92 |
|
| 93 |
Exit-Json -obj $result |