Event Hubs
Receive client events from the PowerShell Universal server.
Event Hubs require a license.
Event Hubs provide the ability to connect client to the PowerShell Universal server. Once connected, the PowerShell Universal server can send messages to the connected clients and they will run a local PowerShell script block.
Creating an Event Hub
To create an event hub, click APIs \ Event Hub and click Create New Event Hub. Event Hubs are named and can choose to enforce authentication and authorization.
Agent
The agent process is responsible for responding to Event Hub requests. You can learn more about installing the agent on our Installation page.
agent.json
After installing the agent, you will need to configure the client by using an agent.json
file. This file should be created in %ProgramData%\PowerShellUniversal
. Changes to this file require a restart of the Agent service.
The installer will not create the folder or file automatically.
This JSON file configures the Agent to connect to the hub and run scripts when invoked.
Options
The below options can be included in the agent.json
file.
Connections
These are the connections to Event Hubs. Each connection can contain it's own URL, hub, authentication and script to execute.
URL (Required)
The URL of the PowerShell Universal service.
Hub (Required)
The name of the Event Hub.
AppToken
The app token used to authentication against the hub.
UseDefaultCredentials
Windows Authentication will be used to authenticate against the hub.
ScriptPath
The script to execute when an event is received. This script is read into memory and not from disk. Variables such as $PSScriptRoot
are currently not supported. This is optional as event hubs can also run commands directly.
Send Events
From within the PowerShell Universal server, you can send events from a hub to connected clients using the Send-PSUEvent
cmdlet.
The -Data
parameter accepts an object and will be serialized using CLIXML and send to the client. The data will be deserialized before passing to the script block.
You can also run commands. This does not require defining a script on the event hub client. You can also use the Invoke-PSUCommand
alias to mimic native PowerShell behavior.
Receive Data from Clients
This feature is only available when sending data to an individual client, rather than all clients connected to a hub.
Example: Running Scripts on Remote Machines
This example provides a way to run scripts on remote machines without having to install another instance of PowerShell Universal.
This example allows for sending scripts to remote machines and executing them with a generic event hub script.
First, create an event hub in PowerShell Universal. This example does not use authentication.
Next, install the Event Hub Client on the remote machine. Create a configuration file in %ProgramData%\PowerShellUniversal\eventHubClient.json
.
Next, create a helper script.ps1 to receive the event hub data and process requests from PSU to invoke scripts. It creates a new temporary PS1 file and uses the $EventData
passed down from the event hub message with the contents and parameters for the script.
In PowerShell Universal, add a script that you want to run on the remote machine. In this example, it simply starts a process.
Finally, add another script that sends the event down to the client. This could be from an API or an App as well. It uses Get-PSUEventHubConnection
to get the target computer’s connection ID and then sends an event with the contents of a script and any parameters for that script. Because the script on event hub side is generic, it will just run whatever is passed to it.
From here you could event use the script to schedule jobs to run on the remote machines using the event hub client.
Last updated