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

[玩转系统] 如何使用 CSOM PowerShell 连接到 SharePoint Online?

作者:精品下载站 日期:2024-12-14 15:30:43 浏览:14 分类:玩电脑

如何使用 CSOM PowerShell 连接到 SharePoint Online?


要求: 使用 PowerShell CSOM 连接到 SharePoint Online。

[玩转系统] 如何使用 CSOM PowerShell 连接到 SharePoint Online?

如何使用 PowerShell 使用 CSOM 连接到 SharePoint Online?

尽管 SharePoint Online Management Shell 提供了多个 PowerShell Cmdlet 来管理 SharePoint Online,但它的局限性很大。实现真正的 SharePoint PowerShell 的唯一方法是使用 SharePoint CSOM。客户端对象模型是服务器对象模型的子集,可用于补充它。

先决条件:下载并安装 SharePoint Online 客户端 SDK

从 https://www.microsoft.com/en-us/download/details.aspx?id=42038 下载并安装 SharePoint Online 客户端组件 SDK。安装后,它会在计算机上的 16 配置单元中创建程序集 (DLL) 文件,位置为 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\。。我>

如何为 SharePoint Online 编写 CSOM PowerShell 脚本?

在 SharePoint 本地部署中,我们必须登录 SharePoint Server 并编写并执行 PowerShell 脚本。由于 SharePoint Online 位于云中,我们现在可以从客户端计算机远程编写和运行 PowerShell 脚本。以下是 CSOM PowerShell 脚本的典型流程:

  • 步骤 1:添加 SharePoint Online CSOM 程序集引用
  • 步骤 2:初始化变量或命令行参数(可选)
  • 第 3 步:创建客户端上下文
  • 第 4 步:将对象加载到变量中
  • 第 5 步:检索对象的属性或调用方法

步骤 1:将 CSOM 引用添加到您的脚本中

第一步,您需要在 PowerShell 脚本中加载 CSOM 库。


Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

虽然脚本中的这两行足以满足大多数操作,但在处理用户配置文件、搜索、托管元数据等服务应用程序时,我们需要引用以下 DLL。

  • 用户配置文件 - Microsoft.SharePoint.Client.UserProfiles.dll
  • 托管元数据 - Microsoft.SharePoint.Client.Taxonomy.dll
  • 搜索 - Microsoft.SharePoint.Client.Search.dll
  • 工作流 - Microsoft.SharePoint.Client.WorkflowServices.dll

第 2 步:初始化变量

比如说,您想要连接到一个站点并获取站点名称!我们需要告诉 PowerShell 脚本我们需要连接哪个站点并检索其属性,不是吗?此步骤完全是可选的。但是,它有助于简化脚本。


#Set Parameters
$SiteUrl = "https://crescent.sharepoint.com/sites/projects"

您还可以在运行时获取参数的值:


#Get Parameter Value
$SiteUrl = Read-Host "Enter the Site URL"

第 3 步:创建客户端上下文

您需要有效的凭据才能连接到 SharePoint Online 网站。您可能希望在运行时获取用于连接的用户名和密码,或者您可以在脚本本身中对凭据进行硬编码!


#Get Credentials to connect to SharePoint Online site
$Cred = Get-Credential
   
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

您可以将要连接的凭据硬编码为:


#Set user name and password to connect
$UserName="[email protected]"
$Password = "Password goes here"

#Create Credential object from given user name and password 
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
   
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Cred

第四步:将必要的对象加载到变量中进行处理

建立上下文后,下一步是将对象加载到变量中。假设您想要连接到一个站点并获取站点标题。我们需要首先加载网络对象!


#Get the Web Object
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

这是另一个例子:


#Create a Variable and Assign it to Lists object of the Web
$Lists = $Ctx.web.Lists
$Ctx.load($Lists) 
$Ctx.ExecuteQuery() 

#Use the Variable to retrieve object properties
$Lists | Select Title, ItemCount 

在 CSOM 中,为了最大限度地减少数据传输,请求被捆绑在一起,然后发送到服务器以使用 ExecuteQuery() 方法执行。

第 5 步:检索对象的属性或调用方法

一旦我们加载了对象,我们就可以访问它的成员来检索它的属性或调用它的方法来执行一些操作。在我们的例子中,我们检索 Web 对象的“Title”属性。


#Retrieve the Title Property of the Web
Write-host $Web.Title

同样,您可以调用对象的任何方法,如下所示:


$Web.Title = "New Web Title"
$Web.Update()
$Ctx.ExecuteQuery()

上述块的完整脚本如下所示:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Set Parameter Value
$SiteUrl = "https://crescent.sharepoint.com/sites/projects"
 
#Get Credentials to connect to SharePoint Online site
$Cred = Get-Credential
   
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the Web Object
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Retrieve the Title Property of the Web
Write-host $Web.Title

您可以使用 Windows PowerShell 控制台、PowerShell ISE 或 Visual Studio Code 执行 SharePoint Online CSOM PowerShell 脚本。

如何在 SharePoint Online PowerShell 脚本中使用凭据管理器?

为了避免凭据弹出窗口,您可以将凭据存储在 Windows 凭据存储中,并在没有提示的情况下连接到 SharePoint Online!这对于无人值守的 PowerShell 脚本非常有用!以下是创建存储凭据的方法:打开控制面板 >> Windows 凭据管理器 >> 选择 Windows 凭据 >> 单击“添加新的通用凭据”>> 输入凭据。我在这里使用“SPO”作为凭证名称。

[玩转系统] 如何使用 CSOM PowerShell 连接到 SharePoint Online?

现在,您可以在 PowerShell 脚本中使用存储的凭据,如下所示:


Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com/sites/Marketing"

#Get Credentials from Windows Credentials Manager
$Cred = Get-PnPStoredCredential -Name "SPO"

#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)

#Get the Web
$Web=$Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

Write-host $Web.Title

同时使用 CSOM 和 SharePoint Online Management Shell

CSOM 和 SharePoint Online Management Shell 可以一起使用!有时,我们可能需要它作为网站集级别的 CSOM 脚本作用域,而 SharePoint Online Management Shell 的作用域为租户级别。例如,您想要检索所有网站集并迭代每个网站集并检索每个网站中的网站数量。以下是如何使用 PowerShell 连接到 SharePoint Online 网站的示例:


#load powershell sharepoint online module
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
 
#Tenant Admin Site URL Parameter
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials
$Cred = Get-Credential

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL -Credential $Cred
 
#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
 
#Traverse through each site collection and get their subsits count
Foreach ($Site in $SiteCollections)
{
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.Url)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Immediate Subsites in the Site
    $Webs = $Ctx.Web.Webs
    $Ctx.Load($webs)
    $Ctx.executeQuery()
  
    Write-host "Number of Sub-sites in $($Site.Url):"$Webs.Count
}

如何获取 SharePoint Online 对象的所有可用方法和属性?

虽然 Microsoft 文档是可靠的来源,但您可以使用 SharePoint Online 客户端浏览器,因为它是探索 CSOM 中对象的绝佳工具。

以下是我的一些 SharePoint Online CSOM PowerShell 脚本:

  • SharePoint Online CSOM 用于更新 PowerShell 中的列表项
  • 用于添加列表项的 SharePoint Online PowerShell CSOM
  • 用于创建文件夹的 SharePoint Online PowerShell CSOM
  • SharePoint Online PowerShell CSOM 创建网站集
  • 用于获取列表项的 SharePoint Online PowerShell CSOM
  • 具有 MFA 的 SharePoint Online Powershell CSOM
  • 用于上传文件的 SharePoint Online Powershell CSOM
  • SharePoint Online CSOM 下载文件 PowerShell

最后但并非最不重要的一点是:SharePoint 模式和实践 (PnP) 包含一个 PowerShell cmdlet 库,允许您使用一个 cmdlet 执行复杂的操作。

概括

总之,使用 CSOM(客户端对象模型)PowerShell 连接到 SharePoint Online 是一个简单的过程,允许您使用 PowerShell cmdlet 在 SharePoint Online 网站上执行各种操作。通过下载并安装 SharePoint Online Management Shell 或 CSOM SDK、导入 SharePoint Online 模块或引用 CSOM 程序集、使用管理员凭据连接到 SharePoint Online 以及运行适当的 cmdlet,您可以自动执行 SharePoint Online 中的任务。请务必注意,您必须拥有必要的权限才能访问 SharePoint Online 并执行您想要执行的操作。通过执行这些步骤,您可以使用 CSOM PowerShell 连接到 SharePoint Online,从而允许您使用 PowerShell cmdlet 在 SharePoint Online 网站上执行各种操作。这可以成为自动化任务、管理站点和内容以及解决问题的强大工具。

经常问的问题:

如何使用 PnP PowerShell 连接到 SharePoint Online?

第一步,您需要使用“Install-Module”PowerShell 命令将 PnP.PowerShell 模块安装到您的计算机上。然后,您可以从 PnP PowerShell 连接到 SharePoint Online 并执行任何操作,例如检索数据、操作对象、更新设置等。
更多信息:如何从 PnP PowerShell 模块连接到 SharePoint Online?

如何从 PowerShell 连接到具有启用 MFA 的帐户的 SharePoint Online?

要使用启用了 MFA 的帐户从 PowerShell 连接到 SharePoint Online,可以选择以下选项:您可以创建应用程序密码,保留“Connect-SPOService”cmdlet 中的 -Credential 参数并尝试连接,使用 PnP PowerShell Connect 的 -Interactive 开关-PnPOnline cmdlet。
更多信息:使用 MFA 从 PowerShell 连接到 SharePoint Online

如何连接到 SharePoint Online Management shell?

安装 SharePoint Online Management Shell 或 Microsoft.Online.SharePoint.PowerShell 模块,然后使用 Connect-SPOService cmdlet 连接到 SharePoint Online。
详细信息:连接到 SharePoint Online Management Shell

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

取消回复欢迎 发表评论:

关灯