1
votes

I am trying to call an Azure function (PowerShell script) triggered by a HTTP url from C# code . I would like to pass some values/parameters to the Azure function from the code.

Ideally, I want to pass some user info to the Azure function and the function would run Connect-PnPOnline with the parameters. So far, the Webhook works but somehow I can't find a way to read data passed from the code. Or probably something went wrong when I tried to send data.

My C# code:

string theUrl = "https://xxxxxx.azurewebsites.net/api/xxxx?code=xxxx==";

var userInfo = new
{
    userId = $"ooooo",
    userPassword = $"xxxxxxxx"
};

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(theUrl));
HttpResponseMessage response = await client.PostAsJsonAsync(theUrl, userInfo);

Powershell Script:

param (
    [string]$userId,
    [string]$userPassword
)
...

How can I read the object passed to the Azure function? Or is there better way to pass data to an Azure PowerShell function?

Thanks!

2

2 Answers

1
votes

The C# code is correct to pass the parameters in the body of request method for POST.

Now in order to read the parameters in the Azure Function, by default all your parameters should be referenced by $Request.

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Read the Parameters as below:

# Interact with query parameters or the body of the request.
$userId = $Request.Query.userId # Query based parameters ()
if (-not $userId) {$userId = $Request.Body.userId} # JSON Body (POST)

$userPassword = $Request.Query.userPassword # Query based parameters ()
if (-not $userPassword) {$userId = $Request.Body.userPassword} # JSON Body (POST)

Once you will have your parameters, you can make use of it in your PowerShell as per your requirement.

More Details on MSDN

0
votes

With POST way, you could use this code to read parameter.

$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$userId = $requestBody.userId

enter image description here