【问题标题】:How to send notification to user/machine in enterprise setting?如何在企业设置中向用户/机器发送通知?
【发布时间】:2016-11-23 07:44:24
【问题描述】:

现状: 我创建了一个 powershell 脚本,它可以在屏幕的右下方绘制一个类似 toast 的通知。我唯一的问题是这只适用于本地计算机。如何使此通知显示在目标计算机上?只需在 Active Directory 中指定机器名称即可?

当然,我愿意接受其他建议。我目前正在阅读 SignalR,但我不确定如何让 SignalR 在本机桌面上工作,而不是通过网站。完美的情况是一个通知系统,可以在 Windows 10 中批量发送 toast 通知。

有什么想法吗?

这是我当前的脚本

Function ShowNotification
{
    [CmdletBinding()]
    param(
           [string] $Title,
           [string] $Message,
           [string] $Image,
           [string] $Hyperlink 
    )
    Add-Type -AssemblyName presentationframework, System.Windows.Forms

         $screenHeight = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenHeight
         $screenWidth = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth

         ############################ NOTIFICATION CLIENT GUI #########################################

$XAML2 = @'
<Window Name="Form2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
        Title="Notification" Height="141.071" Width="504.611" WindowStyle="None" ShowInTaskbar="False" ResizeMode="NoResize" Background="#313130">
    <Window.Effect>
        <DropShadowEffect/>
    </Window.Effect>
    <Grid Margin="-99,133.5,-102,54" Background="#313130">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="238*"/>
            <ColumnDefinition Width="10*"/>
            <ColumnDefinition Width="9*"/>
            <ColumnDefinition Width="190*"/>
            <ColumnDefinition Width="72*"/>
            <ColumnDefinition Width="55*"/>
            <ColumnDefinition Width="132*"/>
        </Grid.ColumnDefinitions>
        <Label Name="TitleLabel" Content="HEY" HorizontalAlignment="Left" Margin="110,-127,0,0" VerticalAlignment="Top" Height="35" Width="388" Grid.ColumnSpan="5" Foreground="White" Background="{x:Null}" FontWeight="Regular" FontSize="18"  FontFamily="Segoe UI SemiLight"/>
        <Path Data="M99,-92.5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="1" Margin="99,-92.5,0,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="1"/>
        <Image Name="imageBox" Source="<PictureSource>" Grid.Column="5" HorizontalAlignment="Left" Height="120" Margin="0,-114,0,-6" VerticalAlignment="Top" Width="75" Grid.ColumnSpan="2"/>
        <TextBlock Name="MessageTB" HorizontalAlignment="Left" Margin="116,-87,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Grid.ColumnSpan="5" Height="80" Width="382" Foreground="White" FontFamily="Segoe UI Light" FontSize="14"/>
    </Grid>
</Window>


'@

[xml]$XAML2 = $XAML2 -replace "<PictureSource>", $Image

        ###############################################################################################

        ############################# CONVERT GUI COMPONENTS TO VARIABLES #############################
        $script:window = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml2))
        $xaml2.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $window.FindName($_.Name) -Scope Script }
        ###############################################################################################

        ############################# EVENT HANDLERS ##################################################
        $window.Add_MouseDown({

            If($Image)
            {
                $img = [System.Drawing.Image]::Fromfile($Image);

                $form = new-object Windows.Forms.Form
                $form.Text = "Image Viewer"
                $form.Width = $img.Size.Height;
                $form.Height =  $img.Size.Width;
                $form.StartPosition = "CenterScreen"

                $pictureBox = new-object Windows.Forms.PictureBox
                $pictureBox.Dock = "Fill"
                $pictureBox.SizeMode = "Zoom"
                $pictureBox.Width =  $img.Size.Width;
                $pictureBox.Height =  $img.Size.Height;

                $pictureBox.Image = $img;
                ### HYPERLINK WHEN CLICKING PICTURE ###
                <#
                If ($img -and $Hyperlink)
                {
                    $pictureBox.Add_Click({
                                ### Opens up IE
                                $ie = New-Object -ComObject InternetExplorer.Application
                                $ie.Navigate($Hyperlink)
                                $ie.Visible = $true
                    })
                }#>

                $form.controls.add($pictureBox)
                $form.Add_Shown( { $form.Activate() } )
                $form.ShowDialog()
            }

            If ($Hyperlink)
            {
                                $ie = New-Object -ComObject InternetExplorer.Application
                                $ie.Navigate($Hyperlink)
                                $ie.Visible = $true
            }

            $window.Close()
        })

    $TitleLabel.Content = $Title
    $MessageTB.Text = $Message

    $window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width)
    $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height)

$timer = new-object System.Windows.Forms.Timer
$timer.Interval = 20000
$timer.Add_Tick({
 $timer.Stop();
    #$timer.Tick -= new EventHandler(formClose_Tick);
$window.Close()
})

$timer.Start()
$window.ShowDialog() | Out-Null

}

【问题讨论】:

  • 嗯,可以分享一下脚本吗?你是如何调用 toast 通知的?

标签: powershell notifications signalr system enterprise


【解决方案1】:

我很久以前写过以下内容(它适用于远程机器 - 在 toast 消息起作用之前,用户必须在机器上登录,但控制台是否被锁定并不重要 - 他们只是需要登录)。 (我不应该发布这个,因为主题启动者没有提供他/她自己的代码,但这次我会例外)。请记住,我在此脚本中有一个变量 $cred 未设置为参数(此变量包含管理员凭据,因为 Invoke-Command 连接到远程计算机)。

Function Invoke-ToastMessage
{
[CmdletBinding()]
    Param
        (
        [string]$Message,
        [string]$Notifier = "Administrators",
        [string]$ComputerName = $null
        )

[scriptblock]$ToastScriptRemote = {
$Message = $args[0]
$Notifier = $args[1]
# XML Template
[xml]$XmlTemplate = @"
<toast scenario="reminder">
  <visual>
    <binding template="ToastGeneric">
      <text>Admin Notification</text>
      <text>$Message</text>
    </binding>
  </visual>
  <actions>
  </actions>
</toast>
"@
    # fake load the assemblies
    [void][Windows.UI.Notifications.ToastNotification,Windows.UI.Notifications,ContentType=WindowsRuntime]
    [void][Windows.Data.Xml.Dom.XmlDocument,Windows.Data.Xml.Dom,ContentType=WindowsRuntime]
    $FinalXML = [Windows.Data.Xml.Dom.XmlDocument]::new()
    $FinalXML.LoadXml($XmlTemplate.OuterXml)
    ## create the toast
    $Toast = [Windows.UI.Notifications.ToastNotification]::new($FinalXML)
    ## Show the TOAST message
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($Notifier).show($Toast)
    }

[scriptblock]$ToastScriptLocal = {
# XML Template
[xml]$XmlTemplate = @"
<toast scenario="reminder">
  <visual>
    <binding template="ToastGeneric">
      <text>Admin Notification</text>
      <text>$Message</text>
    </binding>
  </visual>
  <actions>
  </actions>
</toast>
"@
    # fake load the assemblies
    [void][Windows.UI.Notifications.ToastNotification,Windows.UI.Notifications,ContentType=WindowsRuntime]
    [void][Windows.Data.Xml.Dom.XmlDocument,Windows.Data.Xml.Dom,ContentType=WindowsRuntime]
    $FinalXML = [Windows.Data.Xml.Dom.XmlDocument]::new()
    $FinalXML.LoadXml($XmlTemplate.OuterXml)
    ## create the toast
    $Toast = [Windows.UI.Notifications.ToastNotification]::new($FinalXML)
    ## Show the TOAST message
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($Notifier).show($Toast)
    }

if (![string]::IsNullOrEmpty($ComputerName))
    {
        Invoke-Command -ComputerName $ComputerName -Credential $cred -ScriptBlock $ToastScriptRemote -ArgumentList $Message,$Notifier
    }
    else {$ToastScriptLocal.Invoke()}
}

【讨论】:

  • 嗨。这行得通吗?因为我知道调用 invoke-command 可以很好地运行脚本,但它们不允许显示 GUI 的脚本弹出
  • @bluff 我刚刚测试过!它确实有效。哇。我真的认为您将无法运行带有 GUI 的远程脚本。有没有办法修改显示的吐司,这样我们就可以拥有一个可以打开网页或显示图像等的按钮?谢谢!
  • 您可以嵌入图像(UNC 共享不起作用,但映射驱动器确实起作用)并使用内置的 Windows 声音。添加按钮也是可能的,但是由于您无法将它们连接到某些东西上,因此它们毫无用处。请参阅msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/… 上的文档以获取有关如何形成 XML 以在其中包含图像等的信息。
  • 嗨布拉夫。首先非常感谢您花时间回复。其次,似乎没有办法处理按钮事件???我知道 powershell 无法订阅 WinRT 事件,但我发现这个人为 WinRT 事件创建了一个 .NET 包装器,以便 powershell 可以订阅它们。也许你可以看看并帮助我?我不知道如何使它工作。 deletethis.net/dave/2016-06/WinRT+Toast+from+PowerShell
猜你喜欢
  • 2018-06-13
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
相关资源
最近更新 更多