从 Azure 应用程序和 Runbook 连接 PnPOnline

问题描述 投票:0回答:2

我们有一个 Azure 应用程序注册,并从 Azure RunBook 运行 PowerShell 脚本,以将用户从 AAD 同步到 SharePoint 用户配置文件存储。 该应用程序已批准同意通过 aad 中的图表读取用户以及读取/写入 SharePoint 用户配置文件:

PowerShell 脚本通过应用程序连接到 graph 和 pnponline,运行良好。

$serviePrincipalName = 'ZZZ-SPOScript'
$servicePrincipalConnection=Get-AutomationConnection -Name $serviePrincipalName   
Connect-MgGraph -TenantId $servicePrincipalConnection.TenantId -ClientId $servicePrincipalConnection.ApplicationID -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
Connect-PnPOnline -Url "https://ourtenant-admin.sharepoint.com" -Tenant $servicePrincipalConnection.TenantId -ClientId $servicePrincipalConnection.ApplicationID -Thumbprint $servicePrincipalConnection.CertificateThumbprint

我们可以从aad获取所有用户:

$users = Get-MgUser -All -Property "Id,mail,UserPrincipalName,extension_b8fc35d8e8ec45e689d332303177957a_ZZZ_townCode,extension_b8fc35d8e8ec45e689d332303177957a_ZZZ_costNumber,extension_b8fc35d8e8ec45e689d332303177957a_employeeID,extension_b8fc35d8e8ec45e689d332303177957a_employeeNumber,extension_b8fc35d8e8ec45e689d332303177957a_ZZZ_title,extension_b8fc35d8e8ec45e689d332303177957a_ZZZ_office,extension_b8fc35d8e8ec45e689d332303177957a_extensionAttribute6,extension_b8fc35d8e8ec45e689d332303177957a_extensionAttribute7"

我们迭代所有用户:

foreach($user in $users) {...

但是当我们尝试通过调用从 SharePoint 获取用户配置文件属性时

$fldValue = (Get-PnPUserProfileProperty -Account $user.UserPrincipalName).UserProfileProperties."ZZZ-CostNumber";

我们得到当前用户不是租户管理员。

我们在 RunBook 中运行脚本的服务主体 ZZZ-SPOScript 当然不是租户管理员(并且永远不会)。

因此,我们通过 /_layouts/15/AppInv.aspx 添加了 Azure 应用程序注册的应用程序权限条目

具有以下权限可提升至完全控制

<AppPermissionRequests>
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>

但是我们仍然得到当前用户不是租户管理员。如果脚本尝试调用 获取 PnPUserProfileProperty

我们是否遗漏了什么,或者这可能是 pnponline 中的错误吗?

其他发现:如此处所述,我们应该为社交功能添加完全控制权限。 所以,我将权限更新为

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl" />

但仍然是同样的错误。

powershell sharepoint-online azure-runbook
2个回答
0
投票

看起来第一个字符串中只有一个拼写错误:

$serviePrincipalName = 'ZZZ-SPOScript'

正确的应该是

$servicePrincipalName = 'ZZZ-SPOScript'

0
投票

我正在回答我自己的问题。 首先,我们需要适当的 API 权限来进行应用程序注册:

以及正确的完全控制设置:

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="Read" /></AppPermissionRequests>

其次,我们必须连接应用程序注册的ClientId和ClientSecret。

Connect-PnPOnline -Url $adminUrl -ClientId $clientId -ClientSecret $clientSecret

然后 Get-PnPUserProfileProperty 和 Set-PnPUserProfileProperty 将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.