首页 > 解决方案 > 如何在 Azure Function App 上运行 PnP 脚本

问题描述

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

我创建了一个 Azure Function App 来运行一些 PowerShell 脚本。当我尝试运行我的代码时,它http 502Connect-PnPOnline If I remove处返回错误Connect-PnPOnline,我得到 200 响应。所以我确信它与Connect-PnPOnline我的脚本有关。

我关注了@Lee_MSFT 的帖子, 获取 PowerShell 脚本以在 Azure 上运行 SharePoint 命令 并能够导入模块。

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 
...

我得到了 500Connect-PnPOnline -url $site和 502Connect-PnPOnline -url $site -UseWebLogin

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

标签: azurepowershellsharepoint

解决方案


确保已为 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 - 将值设置为您在第一步中在租户上创建应用程序时复制的客户端密码。

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

使用 PnP PowerShell 进行 SharePoint 操作的 Azure Functions


推荐阅读