我知道在 PowerShell 中使用 CSOM 我们可以从本地计算机连接到 SharePoint Online,我们如何在 PowerShell 中使用 CSOM 连接到内部部署的 SharePoint 2013?
注意:我正在使用 System.Net.NetworkCredential 获取 On-Premsie 的用户名和密码
使用下面的脚本,在执行时出现错误。
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
function OnPremises-Test {
$siteUrl = "https://<>/"
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$clientContext.Credentials = New-Object System.Net.NetworkCredential("myusername", "password")
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
Write-Host " Current web title is '$($web.Title)', $($web.Url)"
}
OnPremises-Test
错误如下:
Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." 在 C:\Users -krirao\Desktop est.ps1:17 char:2 + $clientContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
虽然你的代码在我 2013 年的网站上对我有用 ...Extensions \ISAPI... 不是特定于 SP2013 的,请尝试以下操作;
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
SharePoint 模式和实践 (PnP) 包含一个 PowerShell 命令库 (PnP PowerShell),允许您对 SharePoint 执行复杂的配置和工件管理操作。这些命令使用 CSOM,并且可以针对 both SharePoint Online 和 SharePoint On-Premises。
优点是您编写的代码可以简单地用一个命令行开关替换。