如何在C#中为Azure Function App安装SharePoint PowerShell模块 [英] How to Install SharePoint PowerShell Module inside C# for Azure Function App
问题描述
只是为了管理期望,我是PowerShell和Azure Functions的新手。
Just to manage expectations, I am new to PowerShell and to Azure Functions.
由于Azure Functions 2.x不再支持PowerShell,所以我尝试运行需要C#中的SPO模块的PowerShell脚本(以下代码)我无法运行Azure Function App,因为PowerShell脚本需要SPO模块。有谁知道如何在C#PowerShell实例中安装所需的模块,例如Runspace或类似的东西?我什至在正确的轨道上?任何意见,高度赞赏。谢谢大家。
Since Azure Functions 2.x no longer supports PowerShell so I am trying to run my PowerShell scripts which requires SPO modules from C# (code below) I am having trouble running the Azure Function App because the PowerShell scripts needed the SPO modules. Anybody who knows how to install the needed modules inside C# PowerShell Instance like Runspace or anything the like? I am even on the right track here? Any comments is highly appreciated. Thank you all.
示例代码:
[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
}
}
推荐答案
一旦通过有效负载或Kudo上传模块,您的脚本应进行一些修改。可以使用保存模块直接从 https://www.powershellgallery.com/ 下载这些模块。
之后,您的脚本应该进行一些修改。根据 https:// docs.microsoft.com/zh-CN/powershell/module/sharepoint-pnp/connect-pnponline?view=sharepoint-ps ,-UseWebLogin基于浏览器。或者,您可以使用-Credentials参数。这是构建PSCredential对象的方法:
Once you upload your modules via the payload or Kudo, your script should work with a few modifications. The modules can be directly downloaded from the https://www.powershellgallery.com/ using Save-Module. After that, your script should work with a few modifications. According to https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/connect-pnponline?view=sharepoint-ps, -UseWebLogin is browser based. Alternatively, you can use the -Credentials parameter. Here is how you build a PSCredential object:
$secPassword = ConvertTo-SecureString "StringPassword" -AsPlainText -Force
$credential = [System.Management.Automation.PSCredential]::new("userName", $secPasswd)
在1/23/20更新:
Update on 1/23/20:
截至2019年11月,Functions中的PowerShell具有GA-ed版本,并且在V2和V3中可用/ strong>,有关更多信息,请访问 https: //docs.microsoft.com/zh-cn/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 。
To have your PowerShell function app install dependencies from the https://www.powershellgallery.com/
, you can use our managed dependencies feature. For more info, please visit https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell#dependency-management.
干杯,
Francisco
Francisco
这篇关于如何在C#中为Azure Function App安装SharePoint PowerShell模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!