首页 > 解决方案 > 清除 SPO 用户配置文件的自动化方式

问题描述

作为清理任务的一部分,我正在寻找一种以编程方式从 Sharepoint Online 中的用户配置文件管理器中清除已删除 AAD 帐户的方法。

我正在使用 Sharepoint Powershell 模块 (Microsoft.Online.SharePoint.PowerShell) 手动执行此操作,使用Remove-SPOUserProfile命令行开关,如果我在交互式会话中使用它,它可以完美运行。Connect-SPOService但是,当我尝试在 Azure 自动化中实现我的脚本时,我发现在语句中使用 PSCredential 对象时,特定模块会退回到基本身份验证。并且基本身份验证在我的组织中被阻止(我看不到他们只为我允许它!)

我找到了 PnP 模块 (PnP.PowerShell),它允许通过存储的凭据进行身份验证。但它没有等效的用户配置文件删除 cmdlet。

最后,我尝试使用纯 REST API,虽然我可以获得现有的用户配置文件,但我无法获得已删除帐户的配置文件(在 SPO ProfMngr.aspx 页面中标记为“导入时缺少配置文件” )。这是因为SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)API 需要完全匹配(例如i:0#.f|membership|vardhaman@siteurl.onmicrosoft.com),并且当删除 AAD 帐户时,配置文件用户名会DELETED-<GUID>附加到它。

所以我的问题是:

  1. 我对共享点模块和存储的凭据是对还是错?(IE,因此该模块可以通过服务主体或服务帐户从 Azure 自动化中使用)
  2. 我对 PnP 模块是对还是错,它缺少类似的东西Remove-SPOUserProfile吗?
  3. 使用 REST API,您如何搜索配置文件,尤其是“导入时丢失”的配置文件?
  4. 有什么方法可以预测DELETED-<GUID>给定用户的情况吗?因为如果我查找完整的已删除名称并将其提供给我的 REST 调用,我就能够获得用户配置文件。

关于这方面的官方文档很简单——不再开发旧的传统共享点 API,支持 MS Graph,但 Graph 文档似乎没有涵盖我的特定用例。

任何指针表示赞赏

更新 1

谢谢@Michael Han_MSFT。

我使用的是预发布/每晚构建(0.3.32),但查看发布文档所以没有意识到删除配置文件在那里。

我仍然遇到问题:

Connect-PnPOnline `
-url "https://<tenantname>.sharepoint.com" `
-ClientId $ClientId `
-ClientSecret $ClientSecret

# $guest1 = Guest account's email address

$azureEmail = ($guest1 -replace "@", "_") + "#ext#@<tenantname>.onmicrosoft.com"

Remove-PnPUserProfile `
-LoginName $azureEmail


Remove-PnPUserProfile : The remote server returned an error: (401) Unauthorized.
At line:11 char:1
+ Remove-PnPUserProfile `
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-PnPUserProfile], WebException
    + FullyQualifiedErrorId : System.Net.WebException,PnP.PowerShell.Commands.UserProfiles.RemoveUserProf
   ile

所以我调整了网址:

Connect-PnPOnline `
-url "https://<tenantname>-admin.sharepoint.com" `
-ClientId $ClientId `
-ClientSecret $ClientSecret

$azureEmail = ($guest1 -replace "@", "_") + "#ext#@azurediagovt.onmicrosoft.com"

Remove-PnPUserProfile `
-LoginName $azureEmail

Remove-PnPUserProfile : 
At line:11 char:1
+ Remove-PnPUserProfile `
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Remove-PnPUserProfile], HttpRequestException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.UserProfiles.RemoveUserProfile

所以你可以看到如果我去<tenantname>我得到一个 401,但如果我去<tenantname>-name响应只是空白。

我确定我已为我的应用程序授予了正确的权限(有没有办法查看已分配的权限?

在 AppInv.aspx 我认为有这个权限代码(我关注了几个博客):

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

Get-PnpUserProfileProperty作为进一步的测试,我尝试了我在 REST ( )中所做的 PnP 版本并得到了

Get-PnPUserProfileProperty : Current user is not a tenant administrator.
At line:1 char:1
+ Get-PnPUserProfileProperty -Connection $pnpctx -Account "scottdu@data ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-PnPUserProfileProperty], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.UserProfiles.GetUserProfileProperty

这很奇怪,因为 REST 会给我一个results.d响应。

在这个阶段,我可以考虑让 App Id 成为 Sharepoint 服务管理员(我已经获得批准,允许 Azure 自动化拥有解决此问题所需的任何权利)。

更新 1a:没有区别,除非在分配角色和权限生效之间存在延迟)。

标签: powershellsharepoint-online

解决方案


你可以试试这个命令:Remove-PnPUserProfile

https://pnp.github.io/powershell/cmdlets/Remove-PnPUserProfile.html

您应该安装 PnP.PowerShell 的预发布版本: https ://www.powershellgallery.com/packages/PnP.PowerShell/0.3.8-nightly

更新:

您可以尝试使用租户管理员帐户连接共享点管理站点,然后运行命令 Remove-PnPUserProfile。这对我有用:

在此处输入图像描述


推荐阅读