2

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 Answers 2

2

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
0

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

2
  • I tried your code but it had problem with binding the parameters. I kept having this error [Error] : Cannot bind argument to parameter 'Path' because it is null. . Seems it could't find req..? Do I need to modify the param (...)? Thanks Aug 30, 2019 at 15:51
  • I got it working. I changed the way of passing parameters. HttpWebResponse res = (HttpWebResponse)req.GetResponse(); Thanks a lot. Aug 30, 2019 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.