Powershell未获得SharePoint Online列表

时间:2017-09-20 06:46:42

标签: powershell sharepoint-online

我正在尝试为自动VM备份编写脚本,您也可以在SharePoint Online中手动启动。我打算在SharePoint中保留一个包含VM名称的列表,并希望通过获取VM列表来开始编写脚本。我认为我的代码到目前为止看起来很好,但是当我想按标题获取列表时它什么也没有返回。 这是代码:

[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location))
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location))


Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    return $context
}

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($qry)
    $Context.Load($items)
    $Context.ExecuteQuery()
    return $items 
}

$UserName = read-host "Please enter your name:"
$PasswordReadHost = Read-Host -Prompt "Enter password" -AsSecureString  
$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PasswordReadHost))
$Url = "https://domain.sharepoint.com/"
$ListTitle = "VMList"
$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$items = Get-ListItems -Context $context -ListTitle $ListTitle

当我运行它时,在末尾添加一个断点并检查$list变量的值我收到两条错误消息:

  

使用“0”参数调用“ExecuteQuery”的异常:“列表   带有URL的网站上不存在“VMList”   'https://domain.sharepoint.com'。

  

枚举集合时发生错误:   集合尚未初始化。它没有被请求或   请求尚未执行。它可能需要明确   请求。

经过大量的谷歌搜索,我尝试改变很多东西,但似乎没有任何效果。我想我的问题是:为什么不能在SharePoint上找到列表,初始化集合是什么意思,或者它只是一个授权问题?

0 个答案:

没有答案