【问题标题】:How to Install SharePoint PowerShell Module inside C# for Azure Function App如何在 C# 中为 Azure Function App 安装 SharePoint PowerShell 模块
【发布时间】:2019-01-25 08:41:03
【问题描述】:

只是为了管理期望,我是 PowerShell 和 Azure Functions 的新手。

由于 Azure Functions 2.x 不再支持 PowerShell,因此我尝试运行需要来自 C# 的 SPO 模块的 PowerShell 脚本(以下代码)我在运行 Azure Function App 时遇到问题,因为 PowerShell 脚本需要 SPO 模块。任何知道如何在 C# PowerShell 实例中安装所需模块的人,比如 Runspace 或类似的东西?我什至在正确的轨道上?任何 cmets 都受到高度赞赏。谢谢大家。

示例代码:

 [FunctionName("TestFunction")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                var script = @"
Install-Module Microsoft.Online.SharePoint.PowerShell
Install-Module SharePointPnPPowerShellOnline

$url = '<SharePoint Url>'

$ListName = '<List Name>'

Connect-PnPOnline -Url $url -UseWebLogin 

$Fields = Get-PnPField -List $ListName  
$Items= Get-PnPListItem -List $ListName -PageSize 5000

$RowCtr = 1;

foreach($item in $items.FieldValues)
{
    #do something
    $RowCtr++;
}
";

                PowerShellInstance.AddScript(script);

                var results = PowerShellInstance.Invoke();
                //some other codes
        }
    }

【问题讨论】:

    标签: c# powershell azure-functions azure-functions-runtime


    【解决方案1】:

    您可以在 Azure 函数中使用运行空间,如 this example 第 93-98 行所示。对于模块,您可以将它们作为部署到函数应用的负载的一部分包含在内(推荐),或使用Kudu 安装它们。

    【讨论】:

      【解决方案2】:

      通过有效负载或 Kudo 上传模块后,您的脚本应该可以进行一些修改。可以使用 Save-Module 从https://www.powershellgallery.com/ 直接下载模块。 之后,您的脚本应该可以进行一些修改。根据https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/connect-pnponline?view=sharepoint-ps,-UseWebLogin 是基于浏览器的。或者,您可以使用 -Credentials 参数。以下是构建 PSCredential 对象的方法:

      $secPassword = ConvertTo-SecureString "StringPassword" -AsPlainText -Force
      $credential = [System.Management.Automation.PSCredential]::new("userName", $secPasswd)
      

      20 年 1 月 23 日更新:

      截至 2019 年 11 月,PowerShell in Functions 已 GA-ed,可在 Functions V2 和 V3 中使用,更多信息请访问https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell

      要让您的 PowerShell 函数应用安装来自 https://www.powershellgallery.com/ 的依赖项,您可以使用我们的托管依赖项功能。欲了解更多信息,请访问https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell#dependency-management

      干杯,

      弗朗西斯科

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多