Powershell 如何在企业设置中向用户/机器发送通知?

Powershell 如何在企业设置中向用户/机器发送通知?,powershell,notifications,signalr,system,enterprise,Powershell,Notifications,Signalr,System,Enterprise,当前情况: 我创建了一个powershell脚本,它可以在屏幕右下角绘制类似toast的通知。我唯一的问题是,这只能在本地计算机上工作。如何使此通知显示在目标计算机上?只需在Active Directory中指定机器名 当然,我愿意接受其他建议。我目前正在阅读信号器,但我不确定如何使信号器在本机桌面上工作,而不是通过网站。完美的情况是一个通知系统,可以在Windows10中批量发送toast通知 有什么想法吗 这是我目前的剧本 Function ShowNotification { [C

当前情况: 我创建了一个powershell脚本,它可以在屏幕右下角绘制类似toast的通知。我唯一的问题是,这只能在本地计算机上工作。如何使此通知显示在目标计算机上?只需在Active Directory中指定机器名

当然,我愿意接受其他建议。我目前正在阅读信号器,但我不确定如何使信号器在本机桌面上工作,而不是通过网站。完美的情况是一个通知系统,可以在Windows10中批量发送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

}
函数显示通知
{
[CmdletBinding()]
param(
[字符串]$Title,
[字符串]$Message,
[字符串]$Image,
[字符串]$Hyperlink
)
添加类型-AssemblyName presentationframework,System.Windows.Forms
$screenHeight=获取WmiObject-类Win32_DesktopMonitor |选择对象screenHeight
$screenWidth=获取WmiObject-类Win32_DesktopMonitor |选择对象screenWidth
############################通知客户端GUI#########################################
$XAML2=@'
'@
[xml]$XAML2=$XAML2-替换“,$Image”
###############################################################################################
#############################将GUI组件转换为变量#############################
$script:window=[Windows.Markup.XamlReader]::Load((New Object System.Xml.XmlNodeReader$xaml2))
$xaml2.SelectNodes(“//*[@Name]”)ForEach对象{Set Variable-Name($\.Name)-Value$window.FindName($\.Name)-Scope Script}
###############################################################################################
#############################事件处理程序##################################################
$window.Add\u MouseDown({
如果($Image)
{
$img=[System.Drawing.Image]::Fromfile($Image);
$form=新对象Windows.Forms.form
$form.Text=“图像查看器”
$form.Width=$img.Size.Height;
$form.Height=$img.Size.Width;
$form.StartPosition=“中心屏幕”
$pictureBox=新对象Windows.Forms.pictureBox
$pictureBox.Dock=“填充”
$pictureBox.SizeMode=“缩放”
$pictureBox.Width=$img.Size.Width;
$pictureBox.Height=$img.Size.Height;
$pictureBox.Image=$img;
###单击图片时的超链接###
$form.controls.add($pictureBox)
$form.Add_显示({$form.Activate()})
$form.ShowDialog()
}
如果($Hyperlink)
{
$ie=新对象-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=新对象System.Windows.Forms.timer
$timer.Interval=20000
$timer.Add_Tick({
$timer.Stop();
#$timer.Tick-=新事件处理程序(formClose\u Tick);
$window.Close()
})
$timer.Start()
$window.ShowDialog()| Out Null
}

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

函数调用ToastMessage
{
[CmdletBinding()]
Param
(
[字符串]$Message,
[string]$Notifier=“Administrators”,
[字符串]$ComputerName=$null
)
[scriptblock]$ToastScriptRemote={
$Message=$args[0]
$Notifier=$args[1]
#XML模板
[xml]$XmlTemplate=@”
管理员通知
$Message
"@
#假加载程序集
[void][Windows.UI.Notifications.toast通知,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)
##制作烤面包片
$Toast=[Windows.UI.Notifications.Toast通知]::新建($FinalXML)
##显示祝酒词
[Windows.UI.Notifications.ToastNotificationManager]::CreateTastNotifier($Notifier.show($Toast)
}
[scriptblock]$ToastScriptLocal={
#XML模板
[xml]$XmlTemplate=@”
管理员通知
$Message
"@
#假加载程序集
[void][Windows.UI.Notifications.toast通知,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)
##制作烤面包片
$Toast=[Windows.UI.Notifications.Toast通知]::新建($FinalXML)
##显示祝酒词
[Windows.UI.Notifications.ToastNotificationManager]::CreateTastNotifier($Notify)
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()}
}