繁体   English   中英

在PowerShell中使用Sharepoint CSOM

[英]Using the Sharepoint CSOM with PowerShell

在线有很多关于如何使用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

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

这对我来说是在线的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()

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

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

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

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

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

在您的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,它在我的服务器上不起作用,但在我的工作站上起作用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM