【问题标题】:How do I run PnP Script on Azure Function App如何在 Azure Function App 上运行 PnP 脚本
【发布时间】:2020-01-01 01:52:36
【问题描述】:

我试图让用户通过 Azure 函数运行 powershell 脚本。该脚本应该使用用户提供的一些信息在 SharePoint 上创建一个站点。

我创建了一个 Azure Function App 来运行一些 PowerShell 脚本。当我尝试运行我的代码时,它在Connect-PnPOnline 返回了一个错误http 502 如果我删除 Connect-PnPOnline,我会收到 200 响应。所以我确信它与我脚本中的Connect-PnPOnline 有关。

我关注了@Lee_MSFT Get PowerShell Script to Run SharePoint Commands on Azure 的帖子 并且能够导入模块。

using namespace System.Net

param (
    [string]$projectnumber,
    [string]$projectname
)

$site = "https://xxxxx.sharepoint.com/sites/projects"

$projectsite = -join($site, "/", $projectnumber)
$projecttitle = -join($projectnumber, " ", $projectname)

Connect-PnPOnline -url $site 
...

我从Connect-PnPOnline -url $site 获得了 500,从 Connect-PnPOnline -url $site -UseWebLogin 获得了 502

有人知道为什么我在使用Connect-PnPOnline 时会出现 5xx 错误吗? 非常感谢!

【问题讨论】:

  • 我不认为您可以使用交互式方式在 azure 功能中进行连接。如果您的帐户启用了 MFA,您可能需要使用 AD 应用程序来执行此操作,例如 Connect-PnPOnline -AppId '<id>' -AppSecret '<secret>' -AADDomain 'contoso.onmicrosoft.com'。如果您的帐户未启用 MFA,我认为 Lee_MSFT 提供的方式可以工作。

标签: azure powershell sharepoint


【解决方案1】:

确保您已为 Azure Function 上传 PnP PowerShell 模块,然后使用下面的 PowerShell 连接 SharePoint。

$siteURL = "https://tenant.sharepoint.com/sites/projects"    
$userId = "abc@tenant.onmicrosoft.com"    
$plainText= "*****"  
$pwd = ConvertTo-SecureString $plainText -AsPlainText -Force    
$creds = New-Object System.Management.Automation.PSCredential($userId,$pwd)  
Connect-PnPOnline -Url $siteURL -Credentials $creds 

或者使用这个:

Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url "https://tenant.sharepoint.com/sites/projects" 

SPO_AppId - 将值设置为您在第一步中在租户上创建应用时复制的客户端 ID。

SPO_AppSecret - 将值设置为您在第一步中在租户上创建应用时复制的客户端密码。

我建议你检查下面文章中的步骤。

Azure Functions For SharePoint Operations Using PnP PowerShell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 2023-02-01
    • 2022-11-28
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多