3

网上有很多关于如何通过 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

4 回答 4

4

这适用于我的 SharePoint Online

$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()
于 2013-10-18T20:06:03.830 回答
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属性。

于 2013-08-21T15:41:40.910 回答
2

At the beginning of your powershell script, specify the full path to the CSOM DLLs you need like this:

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'

That resolves the 403 error for me. If I load the dlls using an environment variable as part of the path it doesn't work on my server, but does on my workstation.

于 2017-05-19T13:35:36.440 回答
0

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

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = Get-Credential
$ctx.Credentials = $credentials
...
于 2013-10-31T20:50:09.650 回答