PowerShell: Вывести MessageBox

В Windows 10 Anniversary update у меня перестали работать уведомления типа Toast Notification:

Exception calling «Show» with «1» argument(s): «Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
»
At C:\Program Files (x86)\MyUtils\SetupMode.ps1:88 char:5
+ [Windows.UI.Notifications.ToastNotificationManager]::CreateToastN …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException

Точнее они не работают в этом скрипте, т.к. он запускается с повышением привилегий. Т.е. если я сижу под пользователем с обычными правами, запускаю скрипт, который повышает привилегии до админа (другого пользователя), то происходит такая ошибка. Что-то стало блокировать возможность вызова всплывающего уведомления для другого пользователя.
Пока я не нашёл решение этой проблемы, решил выводить уведомление в виде обычного MessageBox. Правда в самом PowerShell для этого нет встроенного функционала, зато есть возможность призвать на помощь Windows Forms:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Text of the MessageBox","Title of the MessageBox",'Ok')

Для моего скрипта я просто для фрагмента с вызовом уведомления добавил проверку на ошибку, и, в случае её появления, выводил информацию через MessageBox, а не ToastNotification:

# Get an unique AppId from start, and enable notification in registry
if ([System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value.ToString() -eq “S-1-5-18”) 
{
  # Popup alternative when running as system. https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx
  $wshell = New-Object -ComObject Wscript.Shell
  if ($ToastDuration -eq “long”) { $return = $wshell.Popup($ToastText,10,$ToastTitle,0x100) }
  else { $return = $wshell.Popup($ToastText,4,$ToastTitle,0x100) }
}
else 
{
  $AppID = ((Get-StartApps -Name ‘Windows Powershell’) | Select -First 1).AppId
  New-Item “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$AppID” -Force | Out-Null
  Set-ItemProperty “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$AppID” -Name “ShowInActionCenter” -Type Dword -Value “1” -Force | Out-Null
  # Create and show the toast, dont forget AppId
  try 
  {
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID).Show($Toast)
  }
  catch [System.Management.Automation.RuntimeException] #Toast Notification does not work
  {
    # add the required .NET assembly
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show($ToastText,$ToastTitle,'Ok')
  }
}

PowerShell: Вывести MessageBox
или с сообщением об ошибке:
PowerShell: Вывести MessageBox



Подписаться
Уведомление о
guest
0 Комментарий
Inline Feedbacks
View all comments