Guarda una demo×
×

Guarda NinjaOne in azione!

Migliorare la comunicazione IT: Padroneggiare i messaggi pop-up con PowerShell

messaggi pop-up

Punti chiave

  • Comunicazione con l’utente migliorata: Lo script migliora significativamente il modo in cui l’IT comunica le azioni importanti agli utenti.
  • Pop-up personalizzabili: Offre ampie possibilità di personalizzazione per i messaggi pop-up, compresi testo, pulsanti e immagini.
  • Adattabile a varie versioni di Windows: Compatibile con i sistemi Windows moderni e meno recenti, garantendo un’ampia applicabilità.
  • Integrazione dell’immagine Base64: Funzione unica che consente di utilizzare immagini codificate in base64, eliminando la necessità di file esterni.
  • Azioni automatiche dell’utente: Consente risposte automatiche agli utenti, come il riavvio del sistema, migliorando la conformità e l’efficienza.
  • Superiore ai metodi tradizionali: Più efficace e meno dispendioso in termini di tempo rispetto alle e-mail o ai promemoria manuali.
  • Versatilità d’uso: Può essere adattato per una serie di notifiche e azioni oltre ai promemoria per il riavvio.
  • Interfaccia facile da utilizzare: Migliora l’esperienza dell’utente con icone e interfacce personalizzate.
  • Responsabilità durante l’utilizzo: Sottolinea la necessità di un uso responsabile per evitare l’affaticamento da notifica.
  • Possibilità di integrazione con gli strumenti MSP: Può essere integrato con strumenti come NinjaOne per ottimizzare i processi di gestione IT.
  • Importanza dei test e degli aggiornamenti: È essenziale eseguire test approfonditi e mantenere lo script aggiornato per le nuove versioni di Windows.

Nel panorama dinamico della gestione IT, una comunicazione efficace con gli utenti finali è fondamentale. Ciò include la possibilità di notificare tempestivamente le azioni necessarie, come il riavvio o l’aggiornamento del sistema. Gli script PowerShell, che offrono un mix di flessibilità ed efficienza, stanno diventando sempre più la soluzione ideale per queste attività.

Background

Lo script PowerShell fornito è uno strumento avanzato progettato per i fornitori di servizi gestiti (MSP) e i professionisti IT. Consente di visualizzare messaggi pop-up personalizzabili sulle schermate degli utenti. Questo script è particolarmente utile per ricordare o istruire gli utenti a eseguire azioni specifiche, come il riavvio del computer dopo gli aggiornamenti o la manutenzione.

Lo script per messaggi pop-up:

<#
.SYNOPSIS
    Displays a popup on the end user's screen. The script needs to be run as the 'Current Logged In User'. Use "Restart Reminder" to display a request to the end user to restart their computer.
.DESCRIPTION
    Displays a popup on the end user's screen. The script needs to be run as the 'Current Logged In User'. Use "Restart Reminder" to display a request to the end user to restart their computer. 
    See the comment block in this script's code for extra options such as changing the logo or the window title.
    Uses Windows Presentation Framework to accommodate different DPIs and image scaling. On Windows 7 and Server 2008, the script will use the older Windows Form UI.

    You can also convert an image to base64 and replace either lines 164 or 168 if you'd prefer to not have to download an image to a machine. 
.EXAMPLE
    (No Parameters on Windows 11)

    Result: Success

PARAMETER: -RestartReminder
    Displays a generic prompt requesting the user restart their machine. 
    This parameter is equivalent to the below options.
    -Text "Your IT Administrator is requesting that you restart your computer. Click 'Restart Now' after saving your work."
    -ButtonLText "Restart Now"
    -ButtonLaction "shutdown.exe /r /t 30"
    -ButtonRText "Ignore"
    -ButtonRcountdown 900
    -ButtonRDefault
.EXAMPLE
    -RestartReminder (Server 2008)
    
    WARNING: PowerShell 2 cannot import the presentation framework switching to winform...
    Ignore (895) Button Clicked!

PARAMETER: -ApplicationID "ReplaceWithWindowTitle/ApplicationID"
    This will replace the window title and it will also change the taskbar overlay icon and logo/image used in the popup 
    with the Notification Icon used by that Application ID  if it exists and is not overridden by another parameter. 
    This is the same application ID used by the Send-UserPrompt and the built-in Windows notification system.
.EXAMPLE
    -ApplicationID "Contoso Inc"
    
    Application ID with Icon found! Checking if the icon and logo were supplied elsewhere...
    The icon was not specified elsewhere in the script switching from the default icon to the Applications Notification Icon.
    The logo/image was not specified elsewhere in the script switching from the default image to the Applications Notification Icon.
    You can also switch the default Application ID on line 128

PARAMETER: -LogoPath "C:\Replace\This\Path.png"
    The location of an image you would like to use in the large image window.
    This script is running as the end-user so their account will need permission to this path.
    Can be given a URL. Just keep in mind that Ninja doesn't support these special characters: &|;$><`!.
    Can be given any size image however the script will center it and then scale it into a 250px x 125px rectangle. 
    The image will always be centered and a square 1:1 ratio will work.
    Scaling is not done on Server 2008 and Windows 7 (The image is simply centered and you may see it cut off the image if the image is too large).
    
    Supported image formats: .png, .jpg, .ico, .gif (GIFs will be static and not animated.)
    
    You can also convert an image to base64 and replace either lines 165 or 169 if you'd prefer to not have to download an image and give the script a path to it. 

PARAMETER: -IconPath "C:\Replace\This\Path.png"
    The location of an icon you would like to use for the taskbar overlay and window title bar. 
    This script is running as the end-user so their account will need permission to this path.
    Can be given a URL. Just keep in mind that Ninja doesn't support these special characters: &|;$><`!.
    Can be given any icon size however the script will convert it to 64px x 64px so it is recommended to keep to the 1:1 ratio so that the image is not squished.
    
    Supported image formats: .png, .jpg, .ico, .gif (GIFs will be static and not animated.)
    
    You can also convert an image to base64 and replace either lines 164 or 168 if you'd prefer to not have to download an image and give the script a path to it.

PARAMETER: -Text "ReplaceMeWithTextYouWantInsideThePopUp"
    The text you would like displayed inside the popup. 
    If too much text is written, a scrollbar will automatically appear, allowing the end-user to view all the text.

PARAMETER: -AllowResize
    By default, resizing the popup is not allowed. Use -AllowResize to allow resizing the window.

PARAMETER: -ButtonR
    Add a button to the bottom right (can only be specified once).

PARAMETER: -ButtonRdefault
    Set the right button as the default button. This will allow end-users to simply hit the enter key when the window is in focus to perform a click.

PARAMETER: -ButtonRtext "TextYouWouldLikeInsideTheButton"
    Change the text from 'Right Button' to whatever you put encased in quotes.
.EXAMPLE
    -ButtonRtext "Later"

    Later Button Clicked!

PARAMETER: -ButtonRaction "ReplaceWithAnyCMDcommand"
    This will set the action the right button performs when clicked. Can be given any command that will work in cmd.exe as well as parameters. ex. logoff.exe 

PARAMETER: -ButtonRcountdown "160"
    This will add a countdown to the right button with your input in seconds. When the countdown reaches 0 it'll click the button for the end user.
    The left and right button countdown cannot be used at the same time.

PARAMETER: -ButtonL
    Add a button to the bottom left (can only be specified once).

PARAMETER: -ButtonLdefault
    Set the left button as the default button. This will allow end-users to simply hit the enter key when the window is in focus to perform a click.

PARAMETER: -ButtonLtext "TextYouWouldLikeInsideTheButton"
    Change the text from 'Left Button' to whatever you put encased in quotes.
.EXAMPLE
    -ButtonLtext "Later"

    Later Button Clicked!

PARAMETER: -ButtonLaction "ReplaceWithAnyCMDcommand"
    This will set the action the left button performs when clicked. Can be given any command that will work in cmd.exe as well as parameters. ex. logoff.exe 

PARAMETER: -ButtonLcountdown
    This will add a countdown to the left button with your input in seconds. When the countdown reaches 0 it'll click the button for the end user.
    The left and right button countdown cannot be used at the same time.

PARAMETER: -Verbose
    More verbose output (useful for troubleshooting).
.OUTPUTS
  None
.NOTES
  Minimum OS Architecture Supported: Windows 7+, Server 2008+
  Release Notes:
  Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/it/condizioni-utilizzo/
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [Switch]$AllowResize = [System.Convert]::ToBoolean($env:allowEnduserToResizeWindow),
    # You can set line 127 to [String]$ApplicationID = "$env:NINJA_COMPANY_NAME" to automatically use your company name.
    [Parameter()]
    [String]$ApplicationID = "NinjaOne RMM",
    [Parameter()]
    [Switch]$ButtonR,
    [Parameter()]
    [Switch]$ButtonRdefault,
    [Parameter()]
    [String]$ButtonRtext,
    [Parameter()]
    [String]$ButtonRaction,
    [Parameter()]
    [int]$ButtonRcountdown,
    [Parameter()]
    [Switch]$ButtonL,
    [Parameter()]
    [Switch]$ButtonLDefault,
    [Parameter()]
    [String]$ButtonLtext,
    [Parameter()]
    [String]$ButtonLaction,
    [Parameter()]
    [int]$ButtonLcountdown,
    [Parameter()]
    [String]$IconPath,
    [Parameter()]
    [String]$LogoPath,
    [Parameter()]
    [String]$Text,
    [Parameter()]
    [Switch]$winForm,
    [Parameter()]
    [Switch]$RestartReminder = [System.Convert]::ToBoolean($env:restartReminder)
)

begin {

    # You can replace the below line with $IconBase64 = "ReplaceThisWithYourBase64encodedimageEncasedInQuotes" and the script will decode the image and use it 
    # for the taskbar overlay icon and title bar icon.
    $IconBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAJFBMVEUARF0Apc0AmL8ApM0Aos0Aps7///8Am8ia1ug9rtLd8/jw+/2tMDHwAAAABXRSTlMBrBTIcce4nvwAAAIeSURBVHic7dvrcoMgEAXgiOAivv/7Fm+JBpCLwk7bsz86rcNkPw+Y0Gl5vd4lGtbLKSG7vmF18mwQnWpe3YcghP2Z1svU8OtbIOihm8op25M2gWBov9UqYJj/vSRzAGsEkhMglxngWINbdbxLAAAAAAAAAAAAAKAI8Oz2KRtApPWThEyAbT8NZwDZGpeav6sLIKXNMBwAtuGotTGTvTpMRms9qkxEBsDe/dz+A7B3rufeS/utrCKPkAywzfYmK8BeOHY+lBkzBImALfwDgA4XnNLphCTA4e43AKmL9vNMJD8pCQAna20nP5D+SfkQgJyp1qS9PYsEKQDnpVP627WYJCgBmGj+GRmUAFIraSXWBAwDcwJJk1AXMIzcgHgElQHxCGoDohHcBsybgIvPpei70S2A0csuaNkTBRBTbA7uAOb271E0+gWxOSgHfG87yD+wGsCz7fGONNf9iwGTb89DnlkwkUVQCPD2t1sXz9A6gMDT5YsgsggKARljI/vTMkDo7cU3B1USCL+oOwdVAMGF5RlcAxB+tBoBwq/JDlDcAPYEAGgDuPiNBwkgASSABJAAEkACSAAJIAEkgASQABL4JwlcA9w/9N4GTOZcl1OQMTgRoEannhv9O/+PCAAAAAAAAAAAAACAPwhgP+7HeOCR1jOfjBHI9dBrz9W/34/d9jyHLvvPweP2GdCx/3zyvLlAfZ8+l13LktJzAJ+nfgAP50EVLvPsRgAAAABJRU5ErkJggg=="
    
    # You can replace the below line with $LogoBase64 = "ReplaceThisWithYourBase64encodedimageEncasedInQuotes" and the script will decode the image and use it 
    # for the main large image / logo
    $LogoBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAJFBMVEUARF0Apc0AmL8ApM0Aos0Aps7///8Am8ia1ug9rtLd8/jw+/2tMDHwAAAABXRSTlMBrBTIcce4nvwAAAIeSURBVHic7dvrcoMgEAXgiOAivv/7Fm+JBpCLwk7bsz86rcNkPw+Y0Gl5vd4lGtbLKSG7vmF18mwQnWpe3YcghP2Z1svU8OtbIOihm8op25M2gWBov9UqYJj/vSRzAGsEkhMglxngWINbdbxLAAAAAAAAAAAAAKAI8Oz2KRtApPWThEyAbT8NZwDZGpeav6sLIKXNMBwAtuGotTGTvTpMRms9qkxEBsDe/dz+A7B3rufeS/utrCKPkAywzfYmK8BeOHY+lBkzBImALfwDgA4XnNLphCTA4e43AKmL9vNMJD8pCQAna20nP5D+SfkQgJyp1qS9PYsEKQDnpVP627WYJCgBmGj+GRmUAFIraSXWBAwDcwJJk1AXMIzcgHgElQHxCGoDohHcBsybgIvPpei70S2A0csuaNkTBRBTbA7uAOb271E0+gWxOSgHfG87yD+wGsCz7fGONNf9iwGTb89DnlkwkUVQCPD2t1sXz9A6gMDT5YsgsggKARljI/vTMkDo7cU3B1USCL+oOwdVAMGF5RlcAxB+tBoBwq/JDlDcAPYEAGgDuPiNBwkgASSABJAAEkACSAAJIAEkgASQABL4JwlcA9w/9N4GTOZcl1OQMTgRoEannhv9O/+PCAAAAAAAAAAAAACAPwhgP+7HeOCR1jOfjBHI9dBrz9W/34/d9jyHLvvPweP2GdCx/3zyvLlAfZ8+l13LktJzAJ+nfgAP50EVLvPsRgAAAABJRU5ErkJggg=="

    # If script form is used replace the parameters
    if ($env:applicationId -and $env:applicationId -notlike "null") { $ApplicationID = $env:applicationId }
    if ($env:logoPath -and $env:logoPath -notlike "null") { $LogoPath = $env:logoPath }
    if ($env:iconPath -and $env:iconPath -notlike "null") { $IconPath = $env:iconPath }
    if ($env:popupMessage -and $env:popupMessage -notlike "null") { $Text = $env:popupMessage }
    if ($env:rightButtonText -and $env:rightButtonText -notlike "null") { $ButtonRtext = $env:rightButtonText }
    if ($env:rightButtonAction -and $env:rightButtonAction -notlike "null") { $ButtonRaction = $env:rightButtonAction }
    if ($env:rightButtonCountdown -and $env:rightButtonCountdown -notlike "null") { $ButtonRcountdown = $env:rightButtonCountdown }
    if ($env:leftButtonText -and $env:leftButtonText -notlike "null") { $ButtonLText = $env:leftButtonText }
    if ($env:leftButtonAction -and $env:leftButtonAction -notlike "null") { $ButtonLaction = $env:leftButtonAction }
    if ($env:leftButtonCountdown -and $env:leftButtonCountdown -notlike "null") { $ButtonLcountdown = $env:leftButtonCountdown }
    if ($env:defaultButton -and $env:defaultButton -notlike "null") {
        if ($env:defaultButton -eq "Right Button") { $ButtonRdefault = $True }
        if ($env:defaultButton -eq "Left Button") { $ButtonLdefault = $True }
    }

    # Sets the parameters for a generic restart prompt
    if ($RestartReminder) {
        if (-not $ButtonLtext) { $ButtonLtext = "Restart Now" }
        if (-not $ButtonLaction) {
            if (([System.Environment]::OSVersion.Version).Major -ge 10) {
                $ButtonLaction = "shutdown.exe /r /soft /t 30"
            }
            else {
                $ButtonLaction = "shutdown.exe /r /t 30"
            } 
        }
        if (-not $ButtonRtext) { $ButtonRtext = "Ignore" }
        if (-not $ButtonRcountdown) { $ButtonRcountdown = 900 }
        if (-not $Text) { $Text = "Your IT Administrator is requesting that you restart your computer. Click 'Restart Now' after saving your work." }
    }

    # These Assemblies are needed to prepare the images for the form
    Write-Verbose "Adding required assemblies..."
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    # Check if the script was run as the default System User
    function Test-IsSystem {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        return $id.Name -like "NT AUTHORITY*" -or $id.IsSystem
    }

    function Test-PSVersion {
        return ($PSVersionTable.PSVersion.Major)
    }

    function ConvertFrom-Base64 {
        param(
            $Base64,
            $Path
        )
        $bytes = [Convert]::FromBase64String($Base64)

        # This section of code will error out when ran multiple times in the same session. This is from wpf holding onto the file after closing. 
        # The file is unlocked when the powershell session is closed.
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
        [IO.File]::WriteAllBytes($Path, $bytes)
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue
    }

    # There's a lot of ways to create icon files the below method creates a png and then creates an ico file in binary form by creating the header and adding the png's binary at the bottom.
    # Once this has been done we can simply write all the bytes to our new file.
    function ConvertFrom-Image {
        param(
            $ImagePath,
            $Path
        )

        # Grab an instance of the image and blank bitmap
        $image = [Drawing.Image]::FromFile($ImagePath)

        # Resize the image to 255px by 255px while maintaining quality.
        # If you want transparency you'll need an Alpha channel in the pixel format
        $bitmap = New-Object System.Drawing.Bitmap (64, 64, [system.drawing.imaging.PixelFormat]::Format32bppArgb)
        $bitmap.SetResolution(64, 64)

        # Create a graphics object which will be used to resize the image to 255px by 255px
        $graphics = [System.Drawing.Graphics]::FromImage($bitmap)

        # Set some quality settings for the resize operation
        $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
        $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
        $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality

        # Draw the image onto the bitmap
        $graphics.DrawImage($Image, 0, 0, 64, 64)
        
        # Temporarily save the image as a png
        $RandomNumber = Get-Random -Maximum 1000000
        $bitmap.Save("$env:TEMP\image-$RandomNumber.png", [System.Drawing.Imaging.ImageFormat]::Png)
        $png = "$env:TEMP\image-$RandomNumber.png"

        # Build the ico file using the png binary.
        if ($PSVersionTable.PSVersion.Major -gt 5) {
            $pngBytes = Get-Content -Path $png -AsByteStream
        }
        elseif ($PSVersionTable.PSVersion.Major -gt 2) {
            $pngBytes = Get-Content -Path $png -Encoding Byte -Raw
        }
        else {
            $pngBytes = [System.IO.File]::ReadAllBytes($png)
        }
        $icoHeader = [byte[]] @(0, 0, 1, 0, 1, 0)
        $imageDataSize = $pngBytes.Length
        $icoDirectory = [byte[]] @(
            64, 64, # icon size
            0, 0, # color count
            0, 0, # reserved
            0, 0, # hotspot x, hotspot y
            ($imageDataSize -band 0xFF),
            ([Math]::Floor($imageDataSize / [Math]::Pow(2, 8)) -band 0xFF),
            ([Math]::Floor($imageDataSize / [Math]::Pow(2, 16)) -band 0xFF),
            ([Math]::Floor($imageDataSize / [Math]::Pow(2, 24)) -band 0xFF),
            22, 0, 0, 0  # offset to image data
        )
        $iconData = $icoHeader + $icoDirectory + $pngBytes

        # Save the completed icon file and clean up any temporary files.
        # This section of code will error out when ran multiple times in the same session. This is from wpf holding onto the file after closing. 
        # The file is unlocked when the powershell session is closed.
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
        if (Test-Path $Path -ErrorAction SilentlyContinue) { Remove-Item $Path -Force }
        [System.IO.File]::WriteAllBytes($Path, $iconData)

        if (Test-Path $png -ErrorAction SilentlyContinue) { Remove-Item $png -Force }
        $bitmap.Dispose()
        $image.Dispose()
        $graphics.Dispose()
        [System.GC]::Collect()

        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue
    }

    function Invoke-Download {
        param(
            [Parameter()]
            [String]$URL,
            [Parameter()]
            [String]$BaseName,
            [Parameter()]
            [Switch]$SkipSleep
        )
        Write-Host "URL Given, Downloading the file..."

        $SupportedTLSversions = [enum]::GetValues('Net.SecurityProtocolType')
        if ( ($SupportedTLSversions -contains 'Tls13') -and ($SupportedTLSversions -contains 'Tls12') ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12
        }
        elseif ( $SupportedTLSversions -contains 'Tls12' ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
        }
        else {
            # Not everything requires TLS 1.2, but we'll try anyways.
            Write-Warning "TLS 1.2 and or TLS 1.3 isn't supported on this system. This download may fail!"
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                Write-Warning "PowerShell 2 / .NET 2.0 doesn't support TLS 1.2."
            }
        }

        $i = 1
        While ($i -lt 4) {
            if (-not ($SkipSleep)) {
                $SleepTime = Get-Random -Minimum 3 -Maximum 30
                Start-Sleep -Seconds $SleepTime
            }

            Write-Host "Download Attempt $i"

            try {
                $WebClient = New-Object System.Net.WebClient
                $Response = $WebClient.OpenRead($Url)
                $MimeType = $WebClient.ResponseHeaders["Content-Type"]
                $DesiredExtension = switch -regex ($MimeType) {
                    "image/jpeg|image/jpg" { "jpg" }
                    "image/png" { "png" }
                    "image/gif" { "gif" }
                    "image/bmp|image/x-windows-bmp|image/x-bmp" { "bmp" }
                    "image/x-icon" { "ico" }
                    default { 
                        Write-Error "The URL you provided does not provide a supported image type. Image Types Supported: jpg, jpeg, ico, bmp, png and gif. Image Type detected: $MimeType"
                        Exit 1 
                    }
                }
                $Path = "$BaseName.$DesiredExtension"
                $WebClient.DownloadFile($URL, $Path)
                $File = Test-Path -Path $Path -ErrorAction SilentlyContinue
                $Response.Close()
            }
            catch {
                if ($Response) { $Response.Close() }
                Write-Warning "An error has occured while downloading!"
                Write-Warning $_.Exception.Message
            }

            if ($File) {
                $i = 4
            }
            else {
                $i++
            }
        }

        if (-not (Test-Path $Path)) {
            Write-Error "Failed to download file!"
            Exit 1
        }

        $Path
    }

    function Build-WPFform {
        # This is xml I created using visual studio community edition https://visualstudio.microsoft.com/downloads/ I removed some of the lines in "Window" so PowerShell could load it.
        [XML]$form = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="350" WindowStartupLocation="CenterScreen" SizeToContent="Height" ResizeMode="CanMinimize" Topmost="True"
>
    <Window.TaskbarItemInfo>
        <TaskbarItemInfo/>
    </Window.TaskbarItemInfo>
    <Grid>
        <Grid x:Name="ImageGrid" Margin="25,25,25,0" Height="125" MinWidth="250" VerticalAlignment="Top">
        <Image x:Name="Logo" HorizontalAlignment="Center" />
        </Grid>
        <Grid x:Name="ContentGrid" Margin="25,188,25,25">
            <Grid.RowDefinitions>
                <RowDefinition MinHeight="65" />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <ScrollViewer x:Name="ScrollBox" VerticalScrollBarVisibility="Auto" Grid.RowSpan="1" MaxHeight="100" >
                <TextBlock x:Name="TextBlock" TextWrapping="Wrap" FontFamily="Microsoft Sans Serif" FontSize="14" />
            </ScrollViewer>
            <Button x:Name="ButtonR" FontFamily="Lucida Sans Unicode" FontSize="12" Content="Accept" Margin="0,0,0,0" Height="30" Width="114" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.RowSpan="2" Visibility="Hidden" />
            <Button x:Name="ButtonL" FontFamily="Lucida Sans Unicode" FontSize="12" Content="Close" Margin="0,0,0,0" Height="30" Width="114" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.RowSpan="2" Visibility="Hidden" />
        </Grid>
    </Grid>
</Window>
"@

        $NR = (New-Object System.Xml.XmlNodeReader $form)
        # Informs powershell that this xml is actually Xaml (Presentation Framework is needed to load it)
        $Window = [Windows.Markup.XamlReader]::Load($NR)

        # Set's the details of the form (How many buttons? Is One Default? All the options set by parameters)
        if ($ApplicationID) {
            Write-Verbose "Setting window title to $ApplicationID"
            $Window.Title = $ApplicationID
        }

        if ($AllowResize) {
            Write-Verbose "Allowing ReSizing of window..."
            $Window.ResizeMode = "CanResize"
        }

        if ($LogoPath) {
            Write-Verbose "Adding Logo..."
            $Logo = $window.FindName("Logo")
            $Logo.Source = $LogoPath
        }

        if ($IconPath) {
            Write-Verbose "Overlaying tasbar icon and setting icon for window..."
            $Window.Icon = $IconPath
            $Window.TaskbarItemInfo.Overlay = $IconPath
        }

        if ($Text) {
            Write-Verbose "Adding Text Block..."
            $TextBlock = $window.FindName("TextBlock")
            $TextBlock.Text = $Text
        }

        if ($ButtonR -or $ButtonRtext) {
            Write-Verbose "Adding Right Button..."
            $RightButton = $window.FindName("ButtonR")

            if ($ButtonRtext) { 
                Write-Verbose "Setting Right Button Text..."
                $RightButton.Content = $ButtonRtext 
            }
            else {
                Write-Warning "It looks like you forgot to enter in button text using -ButtonRtext 'replaceMeWithText' "
            }

            # To set the timer we'll need to create a timer but on the $Script level 
            if ($ButtonRcountdown) {
                Write-Verbose "Adding a countdown..."
                $ButtonText = $RightButton.Content
                $Script:Timer = New-Object System.Windows.Forms.Timer
                $Timer.Interval = 1000

                Function Timer_Tick() {
                    # Add text with countdown value in the button
                    $RightButton.Content = "$ButtonText ($Script:CountDown)"
                    --$Script:CountDown
                    If ($Script:CountDown -lt 0) {
                        $Script:Timer.Stop()
                        # Once the timer is complete we'll send a click event
                        $RightButton.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs -ArgumentList $([System.Windows.Controls.Button]::ClickEvent)))
                        $Script:Timer.Dispose() 
                    }
                }
                $Script:CountDown = $ButtonRcountdown
                $Script:Timer.Add_Tick({ Timer_Tick })
                $Script:Timer.Start()	
            }

            if ($ButtonRdefault) { $RightButton.IsDefault = $True }

            $RightButton.Visibility = "Visible"

            $RightButton.add_click(
                {
                    # I figured the actual button text would be more informative when looking back
                    Write-Host "$($RightButton.Content) Button Clicked!"
                    if ($buttonRaction) { Start-Process cmd.exe -ArgumentList "/c $buttonRaction" -NoNewWindow }
                    $window.Close()
                }
            )
        }

        if ($ButtonL -or $ButtonLtext) {
            Write-Verbose "Adding Left Button..."

            $LeftButton = $window.FindName("ButtonL")

            if ($ButtonLtext) { 
                Write-Verbose "Adding Left Button Text..."
                $LeftButton.Content = $ButtonLtext 
            }
            else {
                Write-Warning "It looks like you forgot to enter in button text using -ButtonLtext 'replaceMeWithText' "
            }

            if ($ButtonLcountdown) {
                Write-Verbose "Adding Countdown..."
                $ButtonText = $LeftButton.Content
                $Script:Timer = New-Object System.Windows.Forms.Timer
                $Timer.Interval = 1000

                Function Timer_Tick() {
                    # Add text with countdown value in the button
                    $LeftButton.Content = "$ButtonText ($Script:CountDown)"
                    --$Script:CountDown
                    If ($Script:CountDown -lt 0) {
                        $Script:Timer.Stop()
                        $LeftButton.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs -ArgumentList $([System.Windows.Controls.Button]::ClickEvent)))
                        $Script:Timer.Dispose()
                    }
                }
                $Script:CountDown = $ButtonLcountdown
                $Script:Timer.Add_Tick({ Timer_Tick })
                $Script:Timer.Start()	
            }

            if ($ButtonLdefault) { $LeftButton.IsDefault = $True }
            $LeftButton.Visibility = "Visible"

            $LeftButton.add_click(
                {
                    Write-Host "$($LeftButton.Content) Button Clicked!"
                    if ($buttonLaction) { Start-Process cmd.exe -ArgumentList "/c $buttonLaction" -NoNewWindow }
                    $window.Close()
                }
            )
        }

        # Actually displays the form here
        $window.ShowDialog() | Out-Null
    }

    function Build-WinForm {
        # Legacy Windows Form UI. It doesn't scale but looks similar to the one I built with WPF
        Write-Verbose "Legacy System Detected switching to windows forms..."
        Write-Verbose "Building initial window..."
        $form = New-Object System.Windows.Forms.Form
        $form.Height = 360
        $form.Width = 350
        $form.AutoSize = $false
        $form.MaximizeBox = $False
        $form.StartPosition = "CenterScreen"
        $form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
        if ($ApplicationID) { $form.Text = $ApplicationID }
        if (-not $AllowResize) { $form.FormBorderStyle = "FixedDialog" }

        Write-Verbose "Setting window icon to $IconPath"
        if ($IconPath) {
            $form.Icon = New-Object System.Drawing.Icon $IconPath
        }
        
        Write-Verbose "Building Logo..."
        $Logo = New-Object System.Windows.Forms.PictureBox
        $Logo.Location = New-Object System.Drawing.Point(40, 25)
        $Logo.Height = 125
        $Logo.Width = 250
        $Logo.BackColor = [System.Drawing.Color]::Transparent
        $Logo.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::CenterImage
        $Logo.ImageLocation = $LogoPath
        $form.Controls.Add($Logo)

        Write-Verbose "Adding TextBox..."
        $TextBox = New-Object System.Windows.Forms.RichTextBox
        $TextBox.Height = 75
        $TextBox.Width = 275
        $TextBox.Text = $Text
        $TextBox.Font = "Microsoft Sans Serif"
        $TextBox.ShowSelectionMargin = $False
        $TextBox.Tabstop = $False
        $TextBox.ReadOnly = $True
        $TextBox.Location = New-Object System.Drawing.Point(30, 175)
        $form.Controls.Add($TextBox)

        Write-Verbose "Adding Left Button..."
        $LeftFormButton = New-Object System.Windows.Forms.Button
        $LeftFormButton.Location = New-Object System.Drawing.Point(30, 275)
        $LeftFormButton.Height = 25
        $LeftFormButton.Width = 114
        $LeftFormButton.Font = "Lucida Sans Unicode"
        if (-not $ButtonLtext) { $LeftFormButton.Text = "Close" }else { $LeftFormButton.Text = $ButtonLtext }
        $LeftFormButton.Add_Click(
            {
                Write-Host "$($LeftFormButton.Text) Button Clicked!"
                if ($buttonLaction) { Start-Process cmd.exe -ArgumentList "/c $buttonLaction" -NoNewWindow }
                $form.Close()
            }
        )
        if ($ButtonLcountdown) {
            $ButtonText = $LeftFormButton.Text
            $Script:Timer = New-Object System.Windows.Forms.Timer
            $Timer.Interval = 1000

            Function Timer_Tick() {
                # Add text with countdown value in the button
                $LeftFormButton.Text = "$ButtonText ($Script:CountDown)"
                --$Script:CountDown
                If ($Script:CountDown -lt 0) {
                    $Script:Timer.Stop(); 
                    $LeftFormButton.PerformClick();
                    $Script:Timer.Dispose(); 
                }
            }
            $Script:CountDown = $ButtonLcountdown
            $Script:Timer.Add_Tick({ Timer_Tick })
            $Script:Timer.Start()	
        }
        if ($ButtonL) { $form.Controls.Add($LeftFormButton) }

        Write-Verbose "Adding Right Button..."
        $RightFormButton = New-Object System.Windows.Forms.Button
        $RightFormButton.Location = New-Object System.Drawing.Point(190, 275)
        $RightFormButton.Height = 25
        $RightFormButton.Width = 114
        $RightFormButton.Font = "Lucida Sans Unicode"
        if (-not $ButtonRtext) { $RightFormButton.Text = "Accept" }else { $RightFormButton.Text = $ButtonRtext }
        $RightFormButton.Add_Click(
            {
                Write-Host "$($RightFormButton.Text) Button Clicked!"
                if ($buttonRaction) { Start-Process cmd.exe -ArgumentList "/c $buttonRaction" -NoNewWindow }
                $form.Close()
            }
        )
        if ($ButtonRcountdown) {
            $ButtonText = $RightFormButton.Text
            $Script:Timer = New-Object System.Windows.Forms.Timer
            $Timer.Interval = 1000

            Function Timer_Tick() {
                # Add text with countdown value in the button
                $RightFormButton.Text = "$ButtonText ($Script:CountDown)"
                --$Script:CountDown
                If ($Script:CountDown -lt 0) {
                    $Script:Timer.Stop(); 
                    $RightFormButton.PerformClick();
                    $Script:Timer.Dispose(); 
                }
            }
            $Script:CountDown = $ButtonRcountdown
            $Script:Timer.Add_Tick({ Timer_Tick })
            $Script:Timer.Start()	
        }
        if ($ButtonR) { $form.Controls.Add($RightFormButton) }

        $form.Add_Load(
            {
                $form.Activate()
            }
        )

        Write-Verbose "Displaying PopUp..."
        $form.ShowDialog() | Out-Null
        $form.Dispose()
    }

    # If it was we'll error out and enform the technician they should run it as the "Current Logged on User"
    if (Test-IsSystem) {
        Write-Error "This script does not work when ran as system. Use Run As: 'Current Logged on User'."
        exit 1
    }

    # Windows Presentation Framework cannot be imported by PowerShell 2.0.
    if ((Test-PSVersion) -lt 3) {
        Write-Warning "PowerShell 2 cannot import the presentation framework switching to winform..."
        $winForm = $True
    }

    # Inform the technician that having two simultaneous countdowns isn't really a good idea (it would technically work but doesn't really make sense)
    if ($ButtonLcountdown -and $ButtonRcountdown) {
        Write-Error "[Error] -ButtonLcountdown and -ButtonRcountdown cannot be used at the same time!"
        exit 1
    }

    # Informing the technician that this doesn't make sense but I will allow it.
    if ($ButtonRdefault -and $ButtonLdefault) {
        Write-Warning "Looks like you've made both buttons the default?"
    }

    # If the technician forgot to set a button we'll set one.
    if (-not $ButtonL -and ($ButtonLtext -or $ButtonLaction -or $ButtonLDefault -or $ButtonLcountdown)) { $ButtonL = $True }
    if (-not $ButtonR -and ($ButtonRtext -or $ButtonRaction -or $ButtonRDefault -or $ButtonRcountdown)) { $ButtonR = $True }
    
    # If no default button was selected I'll pick for you.
    if (-not $ButtonRDefault -and -not $ButtonLDefault) {
        if ($ButtonL -and $ButtonR) { $ButtonRDefault = $True }
        if ($ButtonL -and -not $ButtonR) { $ButtonLDefault = $True }
        if ($ButtonR -and -not $ButtonL) { $ButtonRDefault = $True }
    }

    # This is where Send-UserPrompt (and the default notification system) store's the icon's path
    $iconUri = Get-ItemProperty "HKLM:\SOFTWARE\Classes\AppUserModelId\$($ApplicationId -replace '\s+','.')" -ErrorAction SilentlyContinue | Select-Object IconUri -ExpandProperty IconUri -ErrorAction SilentlyContinue
    
    # The base64 we set by default. If someone manually entered in their own base64 we'll prefer it over the application ID.
    $defaultImg = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAJFBMVEUARF0Apc0AmL8ApM0Aos0Aps7///8Am8ia1ug9rtLd8/jw+/2tMDHwAAAABXRSTlMBrBTIcce4nvwAAAIeSURBVHic7dvrcoMgEAXgiOAivv/7Fm+JBpCLwk7bsz86rcNkPw+Y0Gl5vd4lGtbLKSG7vmF18mwQnWpe3YcghP2Z1svU8OtbIOihm8op25M2gWBov9UqYJj/vSRzAGsEkhMglxngWINbdbxLAAAAAAAAAAAAAKAI8Oz2KRtApPWThEyAbT8NZwDZGpeav6sLIKXNMBwAtuGotTGTvTpMRms9qkxEBsDe/dz+A7B3rufeS/utrCKPkAywzfYmK8BeOHY+lBkzBImALfwDgA4XnNLphCTA4e43AKmL9vNMJD8pCQAna20nP5D+SfkQgJyp1qS9PYsEKQDnpVP627WYJCgBmGj+GRmUAFIraSXWBAwDcwJJk1AXMIzcgHgElQHxCGoDohHcBsybgIvPpei70S2A0csuaNkTBRBTbA7uAOb271E0+gWxOSgHfG87yD+wGsCz7fGONNf9iwGTb89DnlkwkUVQCPD2t1sXz9A6gMDT5YsgsggKARljI/vTMkDo7cU3B1USCL+oOwdVAMGF5RlcAxB+tBoBwq/JDlDcAPYEAGgDuPiNBwkgASSABJAAEkACSAAJIAEkgASQABL4JwlcA9w/9N4GTOZcl1OQMTgRoEannhv9O/+PCAAAAAAAAAAAAACAPwhgP+7HeOCR1jOfjBHI9dBrz9W/34/d9jyHLvvPweP2GdCx/3zyvLlAfZ8+l13LktJzAJ+nfgAP50EVLvPsRgAAAABJRU5ErkJggg=="
    if ($iconUri) {
        Write-Host "Application ID with Icon found! Checking if the icon and logo were supplied elsewhere..."

        if (-not $IconPath -and $IconBase64 -eq $defaultImg) {
            Write-Host "The icon was not specified elsewhere in the script switching from the default icon to the Applications Notification Icon."
            $IconPath = $iconUri
        }
        else {
            Write-Warning "The icon was already specified either by script parameters or by replacing the default base64. Using that instead." 
        }

        if (-not $LogoPath -and $LogoBase64 -eq $defaultImg) {
            Write-Host "The logo/image was not specified elsewhere in the script switching from the default image to the Applications Notification Icon."
            $LogoPath = $iconUri
        }
        else {
            Write-Warning "The Logo/image was already specified either by script parameters or by replacing the default base64. Using that instead."
        }
    }

    # An actual path to an image is prefered over the base64 encoding
    if ($LogoBase64 -and -not $LogoPath) {
        $LogoPath = "$env:TEMP\ninjarmm-popup-logo.png"
        Write-Verbose "Converting Logo base64 to image and saving to $LogoPath"
        ConvertFrom-Base64 -Base64 $LogoBase64 -Path $LogoPath
    }

    # Windows Forms and Wpf will follow a url for the logo but not the taskbar icon. We'll download it and then set the path for the rest of the script here.
    if ($IconPath -match "^http(.?)://(.*)") {
        $IconPath = Invoke-Download -URL $IconPath -BaseName "$env:TEMP\ninjarmm-popup-icon"
    }

    # This will convert the base64 into an image and save it to the temp folder
    if ($IconBase64 -and -not $IconPath) {
        $IconPath = "$env:TEMP\ninjarmm-popup-icon.png"
        Write-Verbose "Converting Icon base64 to original image and saving to $IconPath..."
        ConvertFrom-Base64 -Base64 $IconBase64 -Path $IconPath
    }

    # An IconPath should now exist whether or not base64 was used or not. This will convert the image to a bitmap image and then into a usable icon
    if ($IconPath) {
        Write-Verbose "Converting image to icon and saving to $env:TEMP\ninjarmm-popup-icon.ico ..."
        ConvertFrom-Image -ImagePath $IconPath -Path "$env:TEMP\ninjarmm-popup-icon.ico"
        $IconPath = "$env:TEMP\ninjarmm-popup-icon.ico"
    }
}
process {
    # The -winForm parameter is both for testing purposes and for older os's
    if (-not $winForm) {

        # Time to start creating the form
        Add-Type -AssemblyName PresentationFramework

        # This section of code will error out when ran multiple times in the same session.
        # This is because you can only enable dpi awareness prior to creating gui objects (DPI awareness should be preserved from the previous running).
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
        
        [System.Windows.Forms.Application]::EnableVisualStyles()
        [System.Windows.Forms.Application]::SetCompatibleTextRenderingDefault($True)

        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue
        
        Write-Verbose "WPF imported successfully using WPF to display PopUp."
        
        Build-WPFform
    }
    else {
        Build-WinForm
    }

    # To prevent the timer from sticking around after the script is done we'll stop it, dispose it and then set it as $null
    Write-Verbose "Cleaning up timer..."
    if ($ButtonRcountdown -or $ButtonLcountdown) {
        $Script:Timer.Stop(); 
        $Script:Timer.Dispose();
        $Script:Timer = $null
        $Script:CountDown = $null
    }
}
end {
    
    
    
}

 

Accedi a oltre 700 script nel Dojo NinjaOne

Ottieni l’accesso

Analisi dettagliata

Funzionalità principale: Lo script per messaggi pop-up utilizza il Windows Presentation Framework per le versioni moderne di Windows e ricorre a Windows Forms per i sistemi più vecchi, come Windows 7 o Server 2008. Viene visualizzato un pop-up con opzioni personalizzabili come pulsanti, timer per il conto alla rovescia e testo personalizzato.

Personalizzazione e flessibilità: Lo script offre vari parametri di personalizzazione, tra cui testo, pulsanti, azioni per ciascun pulsante e timer per il conto alla rovescia. Permette inoltre di utilizzare immagini e icone personalizzate, migliorando l’interfaccia utente.

Codifica Base64 per le immagini: Una caratteristica innovativa è la possibilità di utilizzare immagini codificate in base64 per le icone e i loghi, eliminando la necessità di file immagine esterni.

Variabili d’ambiente e impostazioni predefinite: Lo script per messaggi pop-up può utilizzare le variabili d’ambiente per le impostazioni e i valori predefiniti, semplificando in questo modo la distribuzione su più sistemi.

Casi d’uso potenziali

Immagina un reparto IT che deve ricordare a centinaia di utenti di riavviare il computer dopo un aggiornamento importante. Utilizzando questo script, possono distribuire dei messaggi pop-up con un timer di conto alla rovescia e un pulsante “Riavvia ora”, riducendo al minimo le interruzioni e garantendo la conformità.

Confronti

I metodi tradizionali, come le notifiche via e-mail o i promemoria manuali, sono meno efficaci e richiedono più tempo rispetto a questo approccio automatizzato. Rispetto agli strumenti di terze parti, questo script per messaggi pop-up offre una maggiore personalizzazione e si integra perfettamente nei flussi di lavoro PowerShell esistenti.

Domande frequenti

Q1: Questo script per messaggi pop-up può essere utilizzato su qualsiasi versione di Windows?
R: È progettato per Windows 7 e versioni più recenti, comprese le edizioni server.

Q2: È possibile personalizzare l’aspetto del pop-up?
R: Sì, puoi personalizzare il testo, i pulsanti e persino aggiungere immagini e icone.

Q3: Posso utilizzare questo script per eseguire azioni diverse dai promemoria di riavvio?
R: Assolutamente. Lo script è versatile e può essere adattato a diverse notifiche e azioni.

Implicazioni

Questo script per messaggi pop-up  migliora la comunicazione e la conformità degli utenti, ma comporta anche la responsabilità di un uso appropriato. Un uso eccessivo o scorretto potrebbe portare gli utenti a stancarsi delle notifiche, riducendo l’efficacia degli avvisi importanti.

Raccomandazioni

  • Esegui dei test accurati: Prima della distribuzione diffusa, testa lo script per messaggi pop-up in un ambiente controllato.
  • Istruzione dell’utente: Informa gli utenti su queste notifiche e sulla loro importanza.
  • Aggiornamento e manutenzione: Aggiorna regolarmente lo script per messaggi pop-up e garantisci la compatibilità con le nuove versioni di Windows.

Considerazioni finali

Nel contesto degli MSP e dei reparti IT, strumenti come NinjaOne possono trarre grandi vantaggi dall’integrazione di tali script PowerShell. Semplifica i processi, garantendo una comunicazione efficiente ed efficace con gli utenti, un pilastro nella moderna gestione dell’infrastruttura IT. Questo script per messaggi pop-up di PowerShell rappresenta una fase di transizione verso sistemi IT più interattivi e reattivi, che migliorano la produttività complessiva e la conformità.

Passi successivi

La creazione di un team IT efficiente ed efficace richiede una soluzione centralizzata che funga da principale strumento di erogazione dei servizi. NinjaOne consente ai team IT di monitorare, gestire, proteggere e supportare in termini di assistenza tutti i dispositivi, ovunque essi si trovino, senza la necessità di una complessa infrastruttura locale.

Scopri qualcosa in più su NinjaOne Endpoint Management, fai un tour dal vivo o inizia la tua prova gratuita della piattaforma NinjaOne.

Ti potrebbe interessare anche

Vuoi diventare un Ninja dell’IT?

Scopri come NinjaOne può aiutarti a semplificare le operazioni IT.

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).