在网站集上获取包含5000个项目的列表

时间:2018-05-04 09:25:47

标签: powershell sharepoint-online

我试图获取网站集中的所有列表。

下面的脚本只是获取特定网站集中的列表,这是正常的。

   #environment variables 
   $username = "user.name@xxx.com"
   $password = Read-Host -Prompt "Enter your password: " -AsSecureString
   $url = "https://xxx.sharepoint.com/sites/xxxxxx"

   $securePassword = ConvertTo-SecureString $password -AsPlainText -Force 

   #add SharePoint Online DLL - update the location if required
   $programFiles = [environment]::getfolderpath("programfiles")
   add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'  

   # connect/authenticate to SharePoint Online and get ClientContext object.. 
   $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
   $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
   $ctx.Credentials = $credentials 

   #get all the sub webs
   $Web = $ctx.Web  
   $ctx.Load($web)  
   $ctx.Load($web.Webs)    
   $ctx.executeQuery()

   Write-Host -ForegroundColor Yellow "There are:" $web.Webs.Count "sub webs in this site collection"

   #get all the lists 
   foreach ($subweb in $web.Webs)
   {
       $lists = $subweb.Lists
       $ctx.Load($lists)
       $ctx.ExecuteQuery()


       #output the list details
       Foreach ($list in $lists)
    {
    if ($list.ItemCount -gt 5000)
           { 
    Write-Host -ForegroundColor Yellow "The site URL is" $subweb.Url
    Write-Host "List title is: " $list.Title". This list has: " $list.ItemCount " items"
           }


    }
   }

当我尝试修改它以获取所有网站集中的列表时。它错了。这是我修改的脚本。

   #environment variables 
   $username = "user.name@xxxx.com"
   $password = Read-Host -Prompt "Enter your password: " -AsSecureString
   $url = get-content "C:\Users\user.name\Documents\PowershellScripts\sites.txt"

   $securePassword = ConvertTo-SecureString $password -AsPlainText -Force 

   #add SharePoint Online DLL - update the location if required
   $programFiles = [environment]::getfolderpath("programfiles")
   add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'  

   # connect/authenticate to SharePoint Online and get ClientContext object.. 
   $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
   $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
   $ctx.Credentials = $credentials 

   #get all the sub webs
   $Web = $ctx.Web  
   $ctx.Load($web)  
   $ctx.Load($web.Webs)    
   $ctx.executeQuery()


   foreach($urls in $url)
   {
   Write-Host -ForegroundColor Yellow "There are:" $web.Webs.Count "sub webs in this site collection"
   #get all the lists 
   foreach ($subweb in $web.Webs)
   {
       $lists = $subweb.Lists
       $ctx.Load($lists)
       $ctx.ExecuteQuery()

       #output the list details
       Foreach ($list in $lists)
    {
    if ($list.ItemCount -gt 5000)
           { 
    Write-Host -ForegroundColor Yellow "The site URL is" $subweb.Url
    Write-Host "List title is: " $list.Title". This list has: " $list.ItemCount " items"
           }


        }
   }
   }

这是错误。

  

您无法在空值表达式上调用方法。在   C:\ Users \用户joshua.maniquiz \文件\ PowershellScripts \ GetAllSubWebsandListswithItemCounts.ps1:22   char:1 + $ ctx.executeQuery()+ ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:   InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:   InvokeMethodOnNull此网站集中有:0个子网站   是:此网站集中的0个子网在此有:0个子网   网站集:此网站集中有0个子网站   是:此网站集中的0个子网

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您将单数/复数与$url$urls混合起来是不吉利的。

如果您的凭据与所有网站相匹配且$ctx对象无法卸载/发布,则可能无法正常工作。

#environment variables 
$username = "user.name@xxx.com"
$password = Read-Host -Prompt "Enter your password: " -AsSecureString
$sites = get-content "C:\Users\user.name\Documents\PowershellScripts\sites.txt"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force 

#add SharePoint Online DLL - update the location if required
$programFiles = [environment]::getfolderpath("programfiles")
add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'  

ForEach($site in $sites) {
    # connect/authenticate to SharePoint Online and get ClientContext object.. 
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site) 
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
    $ctx.Credentials = $credentials 

    #get all the sub webs
    $Web = $ctx.Web  
    $ctx.Load($web)  
    $ctx.Load($web.Webs)    
    $ctx.executeQuery()

    Write-Host -ForegroundColor Yellow "There are:" $web.Webs.Count "sub webs in this site collection"
    #get all the lists 
    ForEach ($subweb in $web.Webs) {
        $lists = $subweb.Lists
        $ctx.Load($lists)
        $ctx.ExecuteQuery()

        #output the list details
        ForEach ($list in $lists) {
            if ($list.ItemCount -gt 5000) { 
                Write-Host -ForegroundColor Yellow "The site URL is" $subweb.Url
                Write-Host "List title is: " $list.Title". This list has: " $list.ItemCount " items"
            }
        }
    }
}

答案 1 :(得分:0)

我使用SharePoint PnP PowerShell cmdlets重写了你的脚本来做同样的事情。我想我已经实现了你想要做的事情:)

$urls = Get-Content "C:\Users\user.name\Documents\PowershellScripts\sites.txt"

$username = "user.name@xxxx.com"
$encpassword = Read-Host -Prompt "Enter your password: " -AsSecureString

$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword

foreach($url in $urls) {
    Connect-PnPOnline -Url $url -Credentials $cred

    $rootweb = Get-PnPWeb -Includes Webs

    foreach($web in $rootweb.Webs) {
        $currentWeb = Get-PnPWeb $web.Id -Includes Lists

        foreach($list in $currentWeb.Lists) {
            if ($list.ItemCount -gt 5000) { 
                Write-Host -ForegroundColor Yellow "The site URL is" $currentWeb.Url
                Write-Host "List title is: " $list.Title". This list has: " $list.ItemCount " items"
           }
        }
    }
}

在运行上述脚本之前,您还需要安装SharePoint PnP cmdlet,您可以通过运行

来安装
Install-Module SharePointPnPPowerShellOnline

如果您运行的是Windows 10.在其他情况下,请参阅this页面以获取更多信息。

使用上面的代码,我想说明如何使用更高级别的cmdlet编写相同的东西。

SharePoint PnP PowerShell cmdlet是SharePoint PnP社区创建的cmdlet,可以免费分发。