【问题标题】:Pass values / parameters to Azure Powershell function by HttpTrigger通过 HttpTrigger 将值/参数传递给 Azure Powershell 函数
【发布时间】:2023-03-27 08:05:01
【问题描述】:

我正在尝试调用由 C# 代码中的 HTTP url 触发的 Azure 函数(PowerShell 脚本)。我想从代码中将一些值/参数传递给 Azure 函数。

理想情况下,我想将一些用户信息传递给 Azure 函数,该函数将使用参数运行 Connect-PnPOnline。到目前为止,Webhook 可以工作,但不知何故,我找不到从代码中读取数据的方法。或者当我尝试发送数据时可能出现问题。

我的 C# 代码:

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 脚本:

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

如何读取传递给 Azure 函数的对象?还是有更好的方法将数据传递给 Azure PowerShell 函数?

谢谢!

【问题讨论】:

    标签: c# azure powershell sharepoint azure-functions


    【解决方案1】:

    C#代码正确传递POST的请求方法体中的参数。

    现在为了读取 Azure 函数中的参数,默认情况下,您的所有参数都应由 $Request 引用。

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

    读取参数如下:

    # 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)
    

    获得参数后,您可以根据需要在 PowerShell 中使用它。

    更多详情请关注MSDN

    【讨论】:

      【解决方案2】:

      通过POST方式,你可以使用这段代码来读取参数。

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

      【讨论】:

      • 我试过你的代码,但是绑定参数有问题。我一直有这个错误[Error] : Cannot bind argument to parameter 'Path' because it is null. 。好像找不到req..?我需要修改param (...)吗?谢谢
      • 我搞定了。我改变了传递参数的方式。 HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 非常感谢。
      猜你喜欢
      • 2022-10-07
      • 2023-03-26
      • 2020-08-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2015-07-05
      • 2019-08-27
      相关资源
      最近更新 更多