The fantastic project brought to the Community by Matthew Kelly.
Matthew describes Pode as,
Pode is a Cross-Platform framework for creating web servers to host REST APIs, Web Pages, and SMTP/TCP Servers. Pode also allows you to render dynamic files using .pode files, which are just embedded PowerShell, or other Third-Party template engines. Pluss many more features, including Azure Functions and AWS Lambda support!
I’m excited by the possibilities this foundational project provides to the PowerShell community. Pode is licensed under the MIT license.
Pode is installed as a module from the PowerShell Gallery. Once installed, you work with the software using PowerShell functions, and there are over 200 of them. The source code has several examples to supplement the excellent documentation.
A non-trivial task is to implement a Pode REST server that can be used to integrate Python with PowerShell. in a Windows environment PowerShell has unmatched capabilities to interrogate Windows computers for data. Python and its ecosystem have extensive data manipulation and analysis resources far beyond what can realistically be performed by PowerShell. Combining the two languages via REST has the added benefit of abstracting the data collection from the data analysis. The first step is to configure the Pode server with a few REST endpoints that return a simple Json message.
# Get the root of this server for future use
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module Pode
# create a server, and start listening on port 8086
Start-PodeServer {
Add-PodeEndpoint -Address localhost -Port 8086 -Protocol Http
# can be hit by sending a GET request to "localhost:8086/api/v1/test"
Add-PodeRoute -Method Get -Path '/api/v1/test' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'endpoint' = 'test'; }
}
# can be hit by sending a GET request to "localhost:8086/api/v1/server
Add-PodeRoute -Method Get -Path '/api/v1/server' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'endpoint' = 'server' }
}
# can be hit by sending a GET request to "localhost:8086/api/v1/network"
Add-PodeRoute -Method Get -Path '/api/v1/network' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'endpoint' = 'network' }
}
}
After saving the file, running it in a PowerShell session will start the server. You should see in the console:
Listening on the following 1 endpoint(s) [1 thread(s)]:
- http://localhost:8086/
Now create a python script to test the REST server.
# load the required json packages
import requests
#import json
#import pandas as pd
# Get the data from each of the end points
api_url = "http://localhost:8086/api/v1/test"
test_response = requests.get(api_url)
api_url = "http://localhost:8086/api/v1/server"
svr_response = requests.get(api_url)
api_url = "http://localhost:8086/api/v1/network"
network_response = requests.get(api_url)
# Add the data to an array
data_array = []
data_array.append(test_response.json())
data_array.append(svr_response.json())
data_array.append(network_response.json())
# Print the values out
for line in data_array:
print(line['endpoint'])
The result should be:
test
server
network