【问题标题】:Using the Sharepoint CSOM with PowerShell将 Sharepoint CSOM 与 PowerShell 结合使用
【发布时间】:2023-03-09 01:46:01
【问题描述】:

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

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

【问题讨论】:

  • 谢谢大家。我已经尝试了所有这些方法,但仍然没有奏效,尽管我们确实使用了表单/声明身份验证。无论我尝试什么,这都是我得到的:PS C:\Scripts> $clientContext.Load($web) 找不到“Load”的重载和参数计数:“1”。在 line:1 char:20 + $clientContext.Load

标签: sharepoint powershell csom


【解决方案1】:

这适用于我的 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()

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 我终于准备好再试一次了。我已经更新了我的方法并正在使用其中的一些回复: PS C:\Scripts> AddCSOM PS C:\Scripts> $context = New-Object SharepointClient.PSClientContext("siteCollection") PS C:\Scripts> $credentials = Get-Credential # 这里提示我输入我的域\用户名和密码 PS C:\Scripts> $context.Credentials = $credentials PS C:\Scripts> $context.ExecuteQuery() 异常调用“ExecuteQuery”和“0 " argument(s): "远程服务器返回错误:(403) Forbidden。"
        • 我终于准备好再试一次了。我已经更新了我的方法并正在使用其中的一些回复:>PS C:\Scripts> AddCSOM >PS C:\Scripts> $context = New-Object SharepointClient.PSClientContext("siteCollection") >PS C:\Scripts> $credentials = Get-Credential # 这里提示我输入域\用户名和密码 >PS C:\Scripts> $context.Credentials = $credentials >PS C:\Scripts> $context.ExecuteQuery() >异常调用带有“0”参数的“ExecuteQuery”:“远程服务器返回错误:(403) Forbidden。”现在怎么办?
        猜你喜欢
        • 2023-03-19
        • 2017-11-23
        • 1970-01-01
        • 2017-04-04
        • 2014-03-13
        • 2017-12-01
        • 1970-01-01
        • 2017-02-08
        • 2020-08-10
        相关资源
        最近更新 更多