Lately I was wondering how I can connect from a PowerShell script to a MS flow? Is this possible and how I can transfer variable(s) information from the script towards the Flow and use these values. In the simple blog example I will transfer variable values towards a MS Flow.

What is needed?

TypeNameInfoLicensing
PowerShell Invoke-WebRequest Link n/a
MS FlowTrigger: Request – When a HTTP request is received (PREMIUM)
Used for incoming API calls that could use actions in a Logic App or other API to trigger this flow.
Link Plan 1 (5€/month)

PowerShell script.

<#
    PowerShell code above, or below can be anything. 
    In this example, the values are manual set and variables 
    have been random chosen.
#>
# Variables
$RandomID = "1285"
$Hostname = "SRV-DEMO-01"
$HostStatus = "Running"

<#
    Define Job Parameters.
    ----------------------
#>
$JobUriParameters = @(
    @{ Name = 'ItemID'; Value = $RandomID},
    @{ Name = 'HostName'; Value = $Hostname},
    @{ Name = 'HostStatus'; Value = $HostStatus}
)
#Convert to JSON parameters
$MSFlowParam = ConvertTo-Json -InputObject $JobUriParameters
PowerShell code, containing the variable values and first step to create the JSON output file that will be sent via the API call to the MS Flow, via a POST operation.

Building the Flow?

We will start the MS Flow with the following trigger ” When a HTTP request is received “. This is a PREMIUM connector and therefore we need a minimum PLAN 1. We will receive the HTTP POST URL, when the Flow will be saved for the first time, currently link is not available.

Adding a JSON schema based on the information that will be sent from PowerShell to MS Flow.

PS> 
<#
    PowerShell code above, or below can be anything.
    In this example, the values are manual set and variables
    have been random chosen.
#>
# Variables
$RandomID = "1285"
$Hostname = "SRV-DEMO-01"
$HostStatus = "Running"

<#
    Define Job Parameters.
    ----------------------
#>
$JobUriParameters = @(
    @{ Name = 'ItemID'; Value = $RandomID},
    @{ Name = 'HostName'; Value = $Hostname},
    @{ Name = 'HostStatus'; Value = $HostStatus}
)
#Convert to JSON parameters
$MSFlowParam = ConvertTo-Json -InputObject $JobUriParameters
$MSFlowParam
#Result output
[
    {
        "Value":  "1285",
        "Name":  "ItemID"
    },
    {
        "Value":  "SRV-DEMO-01",
        "Name":  "HostName"
    },
    {
        "Value":  "Running",
        "Name":  "HostStatus"
    }
]

Copy the result output and paste in as a sample JSON payload. This will generated the required JSON schema.

Result JSON schema after adding the sample payload
JSON Schema generated based in the sample payload
Set the method to POST.

As second step we gone filter the array to get the values of each variable that has been propagate by the PowerShell Script.

Completing the PowerShell Script by adding the Invoke-WebRequest, will make an API call to MS Flow and sends the information via a JSON – file.

<#
    PowerShell code above, or below can be anything. 
    In this example, the values are manual set and variables 
    have been random chosen.
#>
# Variables
$RandomID = "1285"
$Hostname = "SRV-DEMO-01"
$HostStatus = "Running"

<#
    Define Job Parameters.
    ----------------------
#>
$JobUriParameters = @(
    @{ Name = 'ItemID'; Value = $RandomID},
    @{ Name = 'HostName'; Value = $Hostname},
    @{ Name = 'HostStatus'; Value = $HostStatus}
)
#Convert to JSON parameters
$MSFlowParam = ConvertTo-Json -InputObject $JobUriParameters

<#
Gets content from a web page on the Internet via 
Default,Delete,Get,Head,Merge,Options,Patch,Post,Put,Trace
#>
Invoke-WebRequest `
    -Uri 'https://xxxx-xx.westeurope.logic.azure.com:443/workflows/axxxx34xxxxe45xxxxx258cxxxxc487e/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=xxxxxxxiVmqoHHuON0xxxxhX89ei3ybFSlsxxxxsyAY' `
    -ContentType "application/json" `
    -Method POST `
    -Body $MSFlowParam

Result of the data operations when filtering the array on the name – value. This gives us the possibility to split the array in smaller array pieces.

So the answer is YES. We can transfer data from a PowerShell script towards Microsoft Flow. How cool is that!

1 comment

  1. great post … i wanted to send an e-mail notification to my variables and it kept running for each property… what i end up doing was initializing a variable before the filter and set variable after the filter… seems kind of long but it works

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: