Powershell script for SharePoint online inventory fails with FullyQualifiedErrorId : IdcrlException

Copper Contributor

Hi All, I've bee tasked with creating a simple report that shows all the sites on SharePoint Online and a total of the content items in each site.  It's just a basic inventory. The Powershell script below works fine in my developer tenant, but when I run it in our test tenant it fails when hitting line 44 which has the command $context.executequery().  Here's the error message:

 

Exception calling "ExecuteQuery" with "0" argument(s): "The sign-in name or
password does not match one in the Microsoft account system."
At D:\projects\sandbox\SPO Powershell\SPOInventory.ps1:44 char:4
+ $context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IdcrlException

The collection has not been initialized. It has not been requested or the
request has not been executed. It may need to be explicitly requested.
At D:\projects\sandbox\SPO Powershell\SPOInventory.ps1:46 char:13
+ ForEach ($List in $Lists)
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializ
edException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitial
izedException

 

I believe this is caused by the fact that in our test tenant we are using ADFS, but I'm not sure of how to fix the powershell script to make it work.  Here is my powershell script:

 

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$AdminUrl = "tenant admin url"
$UserName = "administrator account"
$Password = "the password"

 

$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)

 

 

function Get-SPOWebs(){
param(
$Url = $(throw "Please provide a Site Collection Url"),
$Credential = $(throw "Please provide a Credentials")
)

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $SPOCredentials
$web = $context.Web
$context.Load($web)
$context.Load($web.Webs)
$context.ExecuteQuery()
foreach($web in $web.Webs)
{
Get-SPOWebs -Url $web.Url -Credential $Credential
$web
}
}

function Get-ItemCount($Url) {

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $SPOCredentials
$Lists = $context.web.Lists
$context.Load($Lists)
$context.ExecuteQuery()
$itemCount = 0
ForEach ($List in $Lists)
{
$listTitle = $List.Title
If ($listTitle -ne 'User Information List' -and `
$listTitle -ne 'Work Flow History' -and `
$listTitle -ne 'Images' -and `
$listTitle -ne 'Site Assets' -and `
$listTitle -ne 'Composed Looks' -and `
$listTitle -ne 'Microfeed' -and `
$listTitle -ne 'Workflow Tasks' -and `
$listTitle -ne 'Access Requests' -and `
$listTitle -ne 'Master Page Gallery' -and `
$listTitle -ne 'Web Part Gallery' -and `
$listTitle -ne 'Style Library' -and `
$listTitle -ne 'List Template Library' -and `
$listTitle -ne 'appdata' -and `
$listTitle -ne 'appfiles' -and `
$listTitle -ne 'Content type publishing error log' -and `
$listTitle -ne 'Form Templates' -and `
$listTitle -ne 'Converted Forms' -and `
$listTitle -ne 'Solution Gallery' -and `
$listTitle -ne 'TaxonomyHiddenList' -and `
$listTitle -ne 'Theme Gallery' -and `
$listTitle -ne 'List Template Gallery' -and `
$listTitle -ne 'Maintenance Log Library' -and `
$listTitle -ne 'Cache Profiles' -and `
$listTitle -ne 'Web Template Extensions' -and `
$listTitle -ne 'Content and Structure Reports' -and `
$listTitle -ne 'wfpub')
{
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Context.Load($items)
$Context.ExecuteQuery()
$itemCount += $items.count
}
}
return $itemCount
}


#Retrieve all site collection infos
$filePath = "D:\Projects\Sandbox\SPO Powershell\spositeinventroy.csv"
$filePath2 = "D:\Projects\Sandbox\SPO Powershell\spoiteminventory.csv"
Connect-SPOService -Url $AdminUrl
$sites = Get-SPOSite
$row = "Site Name,Site Path,Site Collection Path,Is Site Collection,Item Count"
Add-Content $filePath $row

foreach ($site in $sites)
{
$itemCount = 0
$siteCol = $site.Url
$itemCount = Get-ItemCount($siteCol)
$row = $site.Title + "," + $site.Url + "," + $siteCol + "," + "Y" + "," + $itemCount
Add-Content $filePath $row
write-host $site.Url " Item Count = " $itemCount
$AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials
foreach($web in $Allwebs)
{
$itemCount = 0
$webURL = $web.Url
$itemCount = Get-ItemCount($webURL)
$row = $web.Title + "," + $web.Url + "," +$siteCol + "," + "N" + "," + $itemCount
Add-Content $filePath $row
write-host $web.Url " Item Count = " $itemCount
}

}

 

like I said above this works fine in my developer tenant, but fails in my test tenant.  I am certain that I am using the proper credentials, but there must be something that I am missing in order to have this work with ADFS.  Any help is appcreciated thank!

 

 

2 Replies
Does your admin account have permissions on the site collections? If not, are you able to create an onmicrosoft service account for that tenant?

Have you thought about using the Graph API from PowerShell for getting SPO reports?