当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

作者:精品下载站 日期:2024-12-15 00:28:20 浏览:16 分类:玩电脑

使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online


要求:使用 PowerShell 中的 Azure Active Directory 应用程序连接到 SharePoint Online。

如何从 PowerShell 使用 Azure 应用程序 ID 连接到 SharePoint Online?

使用 Azure 应用程序 ID 连接到 SharePoint Online 是通过无人值守的 PowerShell 脚本管理 SharePoint Online 环境的好方法。在本文中,我们将介绍使用 PowerShell 中的 Azure 应用程序 ID 连接到 SharePoint Online 的必要步骤。

步骤 1:设置 Azure AD 应用程序 ID

要使用 Azure 应用程序 ID 连接 SharePoint Online,需要执行以下步骤:

  1. 注册 Azure AD 应用程序
  2. 授予应用程序权限
  3. 创建证书并将其上传到Azure App Secret

注册 Azure 应用程序

第一步是在 Azure 应用程序注册中创建一个新应用程序。

  1. 以全局管理员身份登录到 Azure 门户:https://aad.portal.azure.com
  2. 单击“Azure Active Directory”,然后单击“应用程序注册”。
  3. 单击“注册应用程序”或“新注册”按钮。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  4. 输入您的应用程序名称并保留默认选项,然后单击“注册”。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  5. 您将进入应用程序摘要。记下应用程序 ID。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

授予 Azure 应用程序权限

创建应用程序后,我们必须授予对该应用程序的必要访问权限。在我们的示例中,我们计划在 SharePoint Online 的 PowerShell 脚本中使用此应用程序 ID。因此,我们必须授予 SharePoint 应用程序权限:完全控制。

  1. 在创建的应用程序摘要页面中,单击左侧导航链接中的“API 权限”,然后单击“添加权限”。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  2. 在“请求 API 权限”页面中,选择“SharePoint”。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  3. 选择应用程序权限 >> 选择“Sites.FullControl.All”,然后单击“添加权限”。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  4. 单击“授予管理员同意”以同意权限。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

创建证书并上传到App Secret

下一步是为应用程序创建一个秘密。尽管密码可以使用,但与证书相比,它的优势较小。因此,我们需要一个自签名证书来上传到应用程序。


$CertificateName = "SharePoint Online Certificate"
$CertificatePassword = "Password1"

#Get the "Documents" folder
$DocumentsFolder = [Environment]::GetFolderPath("MyDocuments")

#Generate a Self-signed Certificate
$Certificate = New-SelfSignedCertificate -Subject $CertificateName -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -KeyAlgorithm RSA -HashAlgorithm SHA256

#Export the Certificate to "Documents" Folder in your computer
Export-Certificate -Cert $Certificate -FilePath $DocumentsFolder$CertificateName.cer

#Export the PFX File
Export-PfxCertificate -Cert $Certificate -FilePath "$DocumentsFolder$CertificateName.pfx" -Password (ConvertTo-SecureString -String $CertificatePassword -Force -AsPlainText)

有关创建自签名证书的更多信息,请参见:https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-self-signed-certificate

生成证书后,下一步是将证书上传到应用程序密钥。

  1. 转到您的 Azure 应用程序 >> 单击“证书和机密”。
  2. 点击“上传证书”。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  3. 浏览到生成的 CER 文件,然后单击“添加”按钮。

    [玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

  4. 记下“指纹”。这是与证书关联的安全密钥,用于对应用程序进行身份验证。

就这样!完成所有这些步骤后,您将能够使用 PowerShell 中的 Azure 应用程序 ID 连接到 SharePoint Online!

替代方法:PowerShell 注册应用程序、授予权限和客户端密钥

可以使用 PowerShell 脚本自动执行上述步骤,而无需通过 Web 用户界面。以管理员身份打开 PowerShell 控制台并运行以下脚本:


Register-PnPAzureADApp -ApplicationName "SharePointApp" -Tenant "Crescent.com" -Store CurrentUser -SharePointApplicationPermissions "Sites.FullControl.All" -Interactive

此脚本注册一个新的 Azure AD 应用程序,创建一个新的自签名证书,并将其添加到本地证书存储中。它还会将证书上传到 azure 应用程序注册。

[玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

您将收到同意以下权限的提示:“Sites.FullControl.All”。登录并接受权限请求。

[玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

记下应用程序 ID/客户端 ID 和指纹。

[玩转系统] 使用 PowerShell 中的 Azure AD 应用 ID 连接到 SharePoint Online

现在,我们可以继续使用 PnP PowerShell 连接到 SharePoint Online。

步骤 2:使用应用程序 ID 和证书连接到 SharePoint Online

Azure AD 应用程序准备就绪后,您可以通过 PnP PowerShell 连接到 SharePoint Online,如下所示:


#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/retail"
$ClientID = "3735f461-fdb5-4360-8184-b30345e57796"
$ThumbPrint = "EE4C7845D6794F7525C2482551C2AC89F6B9CEE1"
$Tenant = "Crescent.com"

#Connect to SharePoint Online using Certificate
Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Thumbprint $ThumbPrint -Tenant $Tenant

#Get the Site
Get-PnPSite

任何需要使用该应用程序连接到 SharePoint Online 的人都必须首先在其本地计算机中安装证书,然后使用客户端 ID 和证书指纹进行身份验证。

这是我关于使用 ClientID 和客户端密钥与 PnP PowerShell 连接到 SharePoint Online 的另一篇文章

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯