[玩转系统] 如何使用 CSOM PowerShell 连接到 SharePoint Online?
作者:精品下载站 日期:2024-12-14 15:30:43 浏览:14 分类:玩电脑
如何使用 CSOM PowerShell 连接到 SharePoint Online?
要求: 使用 PowerShell CSOM 连接到 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”作为凭证名称。
现在,您可以在 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
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag