Cómo utilizar PowerShell para enviar notificaciones de mensajes a los usuarios de Windows

Dada la constante necesidad de los administradores de sistemas de enviar notificaciones, ya sea sobre mantenimiento programado, actualizaciones de políticas o posibles amenazas a la seguridad, no es de extrañar que el siguiente script de PowerShell sea muy popular. Ofrece la posibilidad de enviar notificaciones de mensajes (pequeños mensajes emergentes) al usuario que haya iniciado sesión en un ordenador con Windows, para que los administradores puedan captar su atención de forma rápida y sencilla.

El script para enviar notificaciones de mensajes a los usuarios de Windows

#Requires -Version 5.1

<#
.SYNOPSIS
    Sends a toast message/notification to the currently signed in user.
.DESCRIPTION
    Sends a toast message/notification to the currently signed in user.
    This defaults to using NinjaOne's logo in the Toast Message, but you can specify any png formatted image from a url.
    You can also specify the "ApplicationId" to any string. The default is "NinjaOne RMM".

    Setup Required: Before sending a toast message, this script needs to be ran with just the Setup parameter to prepare the computer.
     This requires running a the SYSTEM user.

    After Setup: Then the Subject and Message parameters can be used to run the script as the currently signed in user.

.EXAMPLE
     -Setup
    Sets up the registry with the default settings needed to send toast messages.
    Defaults:
        ApplicationId = "NinjaOne RMM"
        ImagePath = "C:UsersPublicPowerShellToastImage.png"
        ImageURL = "http://www.google.com/s2/favicons?sz=128&domain=www.ninjaone.com"
.EXAMPLE
     -Subject "My Subject Here" -Message "My Message Here"
    Sends the subject "My Subject Here" and message "My Message Here" as a Toast message/notification to the currently signed in user.
.EXAMPLE
     -Setup -ApplicationId "MyCompany" -ImageURL "http://www.google.com/s2/favicons?sz=128&domain=www.ninjaone.com" -ImagePath "C:UsersPublicPowerShellToastImage.png"
    Sets up the registry with the custom setting needed to send toast messages. The example below this is what you will need to use to send the toast message.
.EXAMPLE
     -Subject "My Subject Here" -Message "My Message Here" -ApplicationId "MyCompany"
    Sends the subject "My Subject Here" and message "My Message Here" as a Toast message/notification to the currently signed in user.
        ApplicationId: Creates a registry entry for your toasts called "MyCompany".
        ImageURL: Downloads a png image for the icon in the toast message/notification.
        ImagePath: Where the image will be downloaded to that all users will have access to the image.
.OUTPUTS
    None
.NOTES
    If you want to change the defaults then with in the param block.
    ImagePath uses C:UsersPublic as that is accessible by all users.
    If you want to customize the application name to show your company name,
        then look for $ApplicationId and change the content between the double quotes.

    Minimum OS Architecture Supported: Windows 10
    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/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 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(DefaultParameterSetName = "Default")]
param
(
    [Parameter(Mandatory = $true, ParameterSetName = "Default")]
    [string]
    $Subject,
    [Parameter(Mandatory = $true, ParameterSetName = "Default")]
    [string]
    $Message,
    [Parameter(ParameterSetName = "Setup")]
    [switch]
    $Setup,
    [Parameter(ParameterSetName = "Setup")]
    [string]
    $ImageURL = "http://www.google.com/s2/favicons?sz=128&domain=www.ninjaone.com",
    [Parameter(ParameterSetName = "Setup")]
    [ValidateScript({ Test-Path -Path $_ -IsValid })]
    [string]
    $ImagePath = "C:UsersPublicPowerShellToastImage.png",
    [Parameter(ParameterSetName = "Setup")]
    [Parameter(ParameterSetName = "Default")]
    [string]
    $ApplicationId = "NinjaOne RMM"
)

begin {
    function Set-ItemProp {
        param (
            $Path,
            $Name,
            $Value,
            [ValidateSet("DWord", "QWord", "String", "ExpandedString", "Binary", "MultiString", "Unknown")]
            $PropertyType = "DWord"
        )
        # Do not output errors and continue
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
        if (-not $(Test-Path -Path $Path)) {
            # Check if path does not exist and create the path
            New-Item -Path $Path -Force | Out-Null
        }
        if ((Get-ItemProperty -Path $Path -Name $Name)) {
            # Update property and print out what it was changed from and changed to
            $CurrentValue = Get-ItemProperty -Path $Path -Name $Name
            try {
                Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force -Confirm:$false -ErrorAction Stop | Out-Null
            }
            catch {
                Write-Error $_
            }
            Write-Host "$Path$Name changed from $($CurrentValue.$Name) to $((Get-ItemProperty -Path $Path -Name $Name).$Name)"
        }
        else {
            # Create property with value
            try {
                New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $PropertyType -Force -Confirm:$false -ErrorAction Stop | Out-Null
            }
            catch {
                Write-Error $_
            }
            Write-Host "Set $Path$Name to $((Get-ItemProperty -Path $Path -Name $Name).$Name)"
        }
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue
    }
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
    if ($Setup -and ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem -or $(Test-IsElevated))) {
        Set-ItemProp -Path "HKLM:SOFTWAREClassesAppUserModelId$($ApplicationId -replace 's+','.')" -Name "DisplayName" -Value $ApplicationId -PropertyType String
        Invoke-WebRequest -Uri $ImageURL -UseBasicParsing -OutFile $ImagePath
        Set-ItemProp -Path "HKLM:SOFTWAREClassesAppUserModelId$($ApplicationId -replace 's+','.')" -Name "IconUri" -Value "$ImagePath" -PropertyType String
    }
    function Show-Notification {
        [CmdletBinding()]
        Param (
            [string]
            $ToastTitle,
            [string]
            [parameter(ValueFromPipeline)]
            $ToastText
        )

        # Import all the needed libraries
        [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
        [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
        [Windows.System.User, Windows.System, ContentType = WindowsRuntime] > $null
        [Windows.System.UserType, Windows.System, ContentType = WindowsRuntime] > $null
        [Windows.System.UserAuthenticationStatus, Windows.System, ContentType = WindowsRuntime] > $null

        # Make sure that we can use the toast manager, also checks if the service is running and responding
        try {
            $ToastNotifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($ApplicationId)
        }
        catch {
            Write-Error $_
            Write-Host "Failed to create notification."
        }

        # Use a template for our toast message
        $Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02)
        $RawXml = [xml] $Template.GetXml()

        # Edit the template to our liking, in this case just the Title, Message, and path to an image file
        $($RawXml.toast.visual.binding.text | Where-Object { $_.id -eq "1" }).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
        $($RawXml.toast.visual.binding.text | Where-Object { $_.id -eq "2" }).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
        if ($NodeImg = $RawXml.SelectSingleNode('//image[@id = ''1'']')) {
            $NodeImg.SetAttribute('src', $ImagePath) > $null
        }

        # Serialized Xml for later consumption
        $SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
        $SerializedXml.LoadXml($RawXml.OuterXml)

        # Setup how are toast will act, such as expiration time
        $Toast = $null
        $Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
        $Toast.Tag = "PowerShell"
        $Toast.Group = "PowerShell"
        $Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)

        # Show our message to the user
        $ToastNotifier.Show($Toast)
    }
}
process {
    # Make sure that Setup was used and that we are running with elevated privileges
    if ($Setup -and ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem -or $(Test-IsElevated))) {
        Write-Host "Used $ImageURL for the default image and saved to $ImagePath"
        Write-Host "ApplicationID: $ApplicationId"
        Write-Host "System is ready to send Toast Messages to the currently logged on user."
        exit 0
    }
    elseif ($Setup -and -not ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem -or $(Test-IsElevated))) {
        Write-Error "Failed to setup registry."
        Write-Host "Please run script as SYSTEM or as a user with administrator privileges."
        exit 1
    }

    try {
        if ($(Get-ItemPropertyValue -Path "HKLM:SOFTWAREClassesAppUserModelId$($ApplicationId -replace 's+','.')" -Name "DisplayName" -ErrorAction SilentlyContinue) -like $ApplicationId) {
            Show-Notification -ToastTitle $Subject -ToastText $Message -ErrorAction Stop
        }
        else {
            Write-Error "ApplicationId($ApplicationId) was not found in the registry."
            Write-Host "Please run script as an administrator or as the SYSTEM account with the -Setup parameter."
        }
    }
    catch {
        Write-Error $_
        exit 1
    }
    exit 0
}
end {}

 


Accede a más de 300 scripts en el Dojo de NinjaOne

Obtén acceso

Análisis detallado

El script para enviar notificaciones de mensajes a los usuarios se inicia definiendo los parámetros del mensaje de notificación: Subject, Message, Setup, ImageURL, ImagePath y ApplicationId. Tras la definición, el script comprueba si se está ejecutando con derechos elevados (como administrador).

Para la configuración inicial:

  • Crea o actualiza una entrada en el registro para identificar la aplicación que envía la notificación.
  • Descarga una imagen especificada (por defecto, de NinjaOne) que aparecerá en el mensaje de la notificación.
  • Actualiza la ruta de la imagen en el registro.

Posteriormente, cada vez que el script para enviar notificaciones de mensajes a los usuarios se ejecuta para enviar notificaciones de mensajes, busca la entrada en el registro y utiliza el sistema de notificaciones instantáneas de Windows para mostrar el mensaje.

Posibles casos de uso

Digamos que eres un profesional de TI al servicio de una gran empresa. Está a punto de lanzarse una actualización de software crucial y es necesario informar a todos los empleados sobre el posible tiempo de inactividad. En lugar de basarse en los correos electrónicos, que a menudo quedan sin leer, el administrador puede utilizar este script de PowerShell para enviar una notificación directa al ordenador de cada usuario, garantizando la visibilidad y la difusión puntual de la información.

Comparaciones

Aunque existen métodos alternativos para enviar notificaciones de mensajes, como utilizar el comando «msg» o «Net Send», la ventaja de este script para enviar notificaciones de mensajes reside en su enfoque moderno. Los métodos tradicionales envían mensajes de texto sin formato, mientras que este script PowerShell permite incluir contenidos más ricos, como imágenes e identificadores de aplicación personalizados. Además, el script se combina bien con plataformas como NinjaOne, lo que lo hace más cohesivo para las tareas de RMM.

FAQ

  • ¿Puedo personalizar la imagen del mensaje? 
    Sí, puedes especificar cualquier URL de imagen png para personalizarla.
  • ¿Tengo que ejecutar la configuración cada vez? 
    No, la instalación sólo necesita ejecutarse una vez, preferiblemente con derechos administrativos. Los usos posteriores pueden limitarse a enviar el mensaje.
  • ¿Existe alguna limitación del sistema operativo? 
    El script está diseñado para Windows 10 y versiones posteriores.

Implicaciones

Aunque el script simplifica las notificaciones, es crucial tener en cuenta que cualquier script que altere el registro debe ejecutarse con cuidado. Las modificaciones imprecisas pueden tener consecuencias no deseadas en el sistema. Además, para la seguridad informática, garantizar que la fuente del script es fiable es vital para evitar posibles puertas traseras o vulnerabilidades.

Recomendaciones

  • Haz siempre una copia de seguridad del registro antes de realizar cambios.
  • Utiliza primero el script en un entorno de prueba.
  • Evita sobrecargar a los usuarios con demasiados mensajes para prevenir la «fatiga de alertas».

Reflexiones finales

En la era de la comunicación instantánea, herramientas como este script de PowerShell para enviar notificaciones de mensajes encarnan la eficiencia por la que luchan los departamentos de TI. El potencial de integración de NinjaOne con este tipo de scripts pone de relieve la versatilidad de la plataforma, garantizando que los profesionales de TI vayan un paso por delante en la gestión del sistema y la comunicación con los usuarios. Con estas herramientas en su arsenal, los departamentos de TI pueden garantizar que las alertas importantes nunca pasen desapercibidas.

Próximos pasos

La creación de un equipo de TI próspero y eficaz requiere contar con una solución centralizada que se convierta en tu principal herramienta de prestación de servicios. NinjaOne permite a los equipos de TI supervisar, gestionar, proteger y dar soporte a todos sus dispositivos, estén donde estén, sin necesidad de complejas infraestructuras locales.

Obtén más información sobre NinjaOne Endpoint Management, echa un vistazo a un tour en vivoo tu prueba gratuita de la plataforma NinjaOne.

Categorías:

Quizá también te interese…

Ver demo×
×

¡Vean a NinjaOne en acción!

Al enviar este formulario, acepto la política de privacidad de NinjaOne.

Términos y condiciones de NinjaOne

Al hacer clic en el botón «Acepto» que aparece a continuación, estás aceptando los siguientes términos legales, así como nuestras Condiciones de uso:

  • Derechos de propiedad: NinjaOne posee y seguirá poseyendo todos los derechos, títulos e intereses sobre el script (incluidos los derechos de autor). NinjaOne concede al usuario una licencia limitada para utilizar el script de acuerdo con estos términos legales.
  • Limitación de uso: solo podrás utilizar el script para tus legítimos fines personales o comerciales internos, y no podrás compartirlo con terceros.
  • Prohibición de republicación: bajo ninguna circunstancia está permitido volver a publicar el script en ninguna biblioteca de scripts que pertenezca o esté bajo el control de cualquier otro proveedor de software.
  • Exclusión de garantía: el script se proporciona «tal cual» y «según disponibilidad», sin garantía de ningún tipo. NinjaOne no promete ni garantiza que el script esté libre de defectos o que satisfaga las necesidades o expectativas específicas del usuario.
  • Asunción de riesgos: el uso que el usuario haga del script corre por su cuenta y riesgo. El usuario reconoce que existen ciertos riesgos inherentes al uso del script, y entiende y asume cada uno de esos riesgos.
  • Renuncia y exención: el usuario no hará responsable a NinjaOne de cualquier consecuencia adversa o no deseada que resulte del uso del script y renuncia a cualquier derecho o recurso legal o equitativo que pueda tener contra NinjaOne en relación con su uso del script.
  • CLUF: si el usuario es cliente de NinjaOne, su uso del script está sujeto al Contrato de Licencia para el Usuario Final (CLUF).