2016-11-23 22 views
-1

當前情況: 我創建了一個powershell腳本,可以在屏幕右下方繪製類似敬酒的通知。我唯一的問題是,這隻適用於本地計算機。如何將此通知顯示在目標機器上?只需在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 

} 
+0

好吧,你可以分享腳本嗎?你如何調用吐司通知? – 4c74356b41

回答

0

我下面的地久天長的時間前寫的(它的工作原理在遠程計算機上 - 用戶已經在機器上敬酒的消息工作之前簽署的,但控制檯是否被鎖定無關緊要 - 他們只需登錄即可)。 (我不應該發佈這個,因爲主題首發沒有提供他/她自己的代碼,但這次我會例外)。請記住,我在這個腳本中有一個變量$ 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()} 
} 
+0

嗨。這是否工作?因爲我知道調用invoke-command可以很好地運行腳本,但是他們不允許顯示GUI的腳本彈出 –

+0

@bluff我剛測試過它!它確實有效。哇。我真的以爲你不能在GUI上運行遠程腳本。有沒有一種方法可以修改顯示的烤麪包,以便我們可以像打開網頁或顯示圖像等按鈕一樣?謝謝! –

+0

您可以嵌入圖像(UNC共享不起作用,但映射驅動器可以正常工作)並使用內置的Windows聲音。添加按鈕也是可能的,但是因爲你不能將它們掛接到某些東西上,所以它們很沒用。請參閱https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts上的文檔以獲取有關如何形成具有圖像的XML的信息等等。 – bluuf

相關問題