在PowerShell中使用Sharepoint CSOM

时间:2013-08-20 18:33:25

标签: sharepoint powershell csom

在线有很多关于如何使用PowerShell访问/使用SharePoint客户端对象模型的示例。但是,当然,它们似乎并不适合我。我似乎无法访问某些凭据代码:

PS C:\Scripts> $webUrl = "https://abc.sharepoint.com>"
PS C:\Scripts> $username = "user3"
PS C:\Scripts> $password = "password"
PS C:\Scripts>
PS C:\Scripts> $ctx = new-object Microsoft.SharePoint.Client.ClientContext($webUrl)
PS C:\Scripts> $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
New-Object : Cannot find type Microsoft.SharePoint.Client.SharePointOnlineCredentials]: make sure the assembly containing this type is loaded.
At line:1 char:30
+ $ctx.Credentials = New-Object <<<<  Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

我正在尝试访问我们维护的需要登录身份验证的SharePoint 2010服务器。有谁知道我做错了什么?

好的,很多回复告诉我,我正在使用不正确的凭据类型进行此连接。所以我改为:

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$clientContext.AuthenticationMode = "FormsAuthentication"
$clientContext.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo("myDomain\myUser", "myPassword")

似乎工作正常。但是......然后......

$web = $clientContext.Web
$properties = $web.AllProperties
$clientContext.Load($web)

给了我:

> Cannot find an overload for "Load" and the argument count: "1". At
> line:1 char:20
> + $clientContext.Load <<<< ($web)
>     + CategoryInfo          : NotSpecified: (:) [], MethodException
>     + FullyQualifiedErrorId : MethodCountCouldNotFindBest

当我尝试查看$ clientContent对象时:

PS C:\Scripts> $clientContent | get-member
Get-Member : No object has been specified to the get-member cmdlet.
At line:1 char:28
+ $clientContent | get-member <<<<
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

根本没有任何意义。有人对此有任何帮助吗?

4 个答案:

答案 0 :(得分:4)

这适用于我在线的SharePoint

$siteUrl = “https://URL.sharepoint.com”
$password = Read-Host -Prompt "Enter password" -AsSecureString 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials(
                                                            "Username@URL.onmicrosoft.com"
                                                          , $password) 

$ctx.Credentials = $credentials

$web = $ctx.Web 
$ctx.Load($web) 
$ctx.ExecuteQuery()

答案 1 :(得分:3)

SharePointOnlineCredentials用于对Office365 / SharePoint 2013 Online服务进行身份验证。如果您没有托管的2013实例,PS无法找到该类型也就不足为奇了。

如果您正在使用自己域中维护的服务器并且使用Windows身份验证,则您的上下文将获取现有的安全令牌。

如果您正在进行基于表单或声明的身份验证,则必须告诉您的上下文:

$ctx.AuthenticationMode = "FormsAuthentication"
$ctx.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo("domain\username", "password")

如果您需要输入备用凭据,还有ClientContext.Credentials属性。

答案 2 :(得分:2)

在您的PowerShell脚本的开头,为您需要的CSOM DLL指定 完整路径 ,如下所示:

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

这解决了我的403错误。如果我使用环境变量作为路径的一部分加载dll,它在我的服务器上不起作用,但在我的工作站上起作用。

答案 3 :(得分:0)

我正在使用以下成功对抗SharePoint 2013内部部署安装。它与您的示例略有不同,因为提示提供凭据 - 但我认为这通常比在脚本中输入密码更好。

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = Get-Credential
$ctx.Credentials = $credentials
...