2017-04-20 48 views
0

使用CSOM據我所知,在PowerShell中使用CSOM我們可以連接到SharePoint Online中從本地機器上,我們如何能夠連接到SharePoint 2013的內部部署在PowerShell中使用CSOM?如何連接到SharePoint 2013的內部部署在PowerShell中

注:我使用System.Net.NetworkCredential獲取用戶名和密碼在-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 

以下是在錯誤:

異常調用 「的executeQuery」 與 「0」 的參數(一個或多個): 「的遠程服務器返回錯誤:禁止(403)」 在C:\用戶\ V-krirao \桌面\ test.ps1:17字符:2 + $ clientContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:引發WebException

回答

0

的SharePoint模式與實踐(PNP)包含的PowerShell的文庫命令(PnP PowerShell),該允許您對SharePoint執行復雜的配置和工件管理操作。該命令使用CSOM,可以對着幹SharePoint Online中的SharePoint內部部署

的好處是你寫的可以用一個單一的命令行簡單地更換代碼。

+0

我有PnP的問題,因爲我必須將腳本切換到其他人將部署的不同環境,無法要求他們安裝PnP cmdlet。 – krishna

+0

我知道你必須將其提交給其他環境。如果可能的話,安裝pnp只需使用一個命令即可完成:「Install-Module SharePointPnPPowerShell2013」​​,您可以將其作爲腳本的一部分。如果安裝了PNP,請檢查一下。如果沒有,運行給定的命令。 – Vaibhav

+0

我一直默默使用pnp,但經過一些實驗後,我認爲這是一個有用且有據可查的框架。但它本身並不回答OP。 –

0

雖然你的代碼對我的作品在我網站2013 ... 擴展\ 16 \ 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" 
相關問題