All documentation

Creating a PowerShell API Server with PowerShell Pode and Integrating with Power Automate

Published July 12, 2023 Guide

PowerShell is a versatile scripting language that allows automation and management of various tasks. In this tech article, we will explore how you can use PowerShell Pode, a lightweight web server framework for PowerShell, to create an API server and run custom PowerShell scripts. We will also discuss how this approach can be useful in conjunction with Power Automate's custom connectors to call the API and automate tasks.


Overview of PowerShell Pode and API Development
PowerShell Pode is a powerful framework that enables the creation of web servers using PowerShell. It provides a simple and intuitive way to define routes, handle requests, and respond with custom PowerShell script executions or data retrieval.


Setting Up the PowerShell API Server
Follow these steps to set up a PowerShell API server using PowerShell Pode:

Step 1: Install PowerShell Pode

  1. Open a PowerShell session with administrative privileges.
  2. Install PowerShell Pode by running the command: Install-Module -Name Pode -Scope CurrentUser

Step 2: Create a PowerShell Pode Script

  1. Create a new PowerShell script file, e.g., api-server.ps1.
  2. Import the PowerShell Pode module using the Import-Module cmdlet.
  3. Define the routes and corresponding script execution or data retrieval logic using the Add-PodeRoute cmdlet.
    Example:
   Import-Module Pode
   Add-PodeRoute -Method Get -Path '/api/customscript' -ScriptBlock {
       # Custom PowerShell script logic goes here
       $result = Invoke-Expression -Command 'Write-Output "Hello, World!"'
       $response = @{
           status  = 200
           headers = @{ 'Content-Type' = 'application/json' }
           body    = $result
       }
       return $response
   }

Step 3: Start the API Server

  1. In the script, add the Start-PodeServer command to start the API server.
    Example:
   Start-PodeServer -Port 8080 -IPAddress '127.0.0.1'

Step 4: Run the PowerShell Script

  1. Execute the PowerShell script by running .\api-server.ps1 in your PowerShell session.
  2. The API server will be up and running, ready to respond to incoming requests.


Example API Call to the PowerShell API Server To demonstrate the capabilities of the PowerShell API server, consider making an API call to the /api/customscript endpoint. Here's an example using PowerShell's Invoke-RestMethod cmdlet:

$apiUrl = 'http://localhost:8080/api/customscript'
$response = Invoke-RestMethod -Uri $apiUrl -Method Get

# Display the response
Write-Output $response

Upon executing this code, the PowerShell API server will execute the custom script associated with the /api/customscript endpoint, and the response "Hello, World!" will be displayed in the console.


Integrating with Power Automate's Custom Connector
Power Automate allows you to create custom connectors to interact with external systems and APIs. You can leverage the PowerShell API server created with PowerShell Pode by following these steps:

Step 1: Create a Custom Connector in Power Automate

  1. Open Power Automate and navigate to the Custom Connectors section.
  2. Create a new custom connector and define the API endpoint, methods, and request/response parameters based on your PowerShell Pode API server.

Step 2: Configure Actions and Triggers

  1. Define actions and triggers in the custom connector, mapping them to the corresponding PowerShell Pode API routes.
  2. Specify the necessary input parameters for each action or trigger.

Step 3: Use the Custom Connector in Power Automate Flows

  1. Create a new flow in Power Automate and include the custom connector as a trigger or action.
  2. Configure the required parameters and utilize the response from the PowerShell Pode API server in subsequent steps of the flow.

Benefits and Use Cases
Using PowerShell Pode to create a PowerShell API server and integrating it with Power Automate's custom connectors offers several benefits and use cases, including:

  1. Automation of complex tasks: Execute custom PowerShell scripts remotely through the API server, enabling the automation of various tasks or processes.
  2. Integration with other systems: Use the API server to bridge PowerShell functionality with other systems and services, making it easier to interact and exchange data.
  3. Enhanced extensibility: Extend the capabilities of Power Automate by leveraging PowerShell scripts and custom connectors, enabling the integration of PowerShell-based solutions within your workflows.

By leveraging PowerShell Pode to create a PowerShell API server and integrating it with Power Automate's custom connectors, you can unlock powerful automation capabilities and streamline workflows. This approach allows you to execute custom PowerShell scripts remotely, interact with external systems, and enhance the extensibility of your automation processes. Experiment with PowerShell Pode and Power Automate to explore the possibilities of PowerShell-driven automation and integration.


Hope you found this useful !