Powershell script for SharePoint online inventory fails with FullyQualifiedErrorId : IdcrlException

%3CLINGO-SUB%20id%3D%22lingo-sub-313976%22%20slang%3D%22en-US%22%3EPowershell%20script%20for%20SharePoint%20online%20inventory%20fails%20with%20FullyQualifiedErrorId%20%3A%20IdcrlException%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-313976%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%20I've%20bee%20tasked%20with%20creating%20a%20simple%20report%20that%20shows%20all%20the%20sites%20on%20SharePoint%20Online%20and%20a%20total%20of%20the%20content%20items%20in%20each%20site.%26nbsp%3B%20It's%20just%20a%20basic%20inventory.%20The%20Powershell%20script%20below%20works%20fine%20in%20my%20developer%20tenant%2C%20but%20when%20I%20run%20it%20in%20our%20test%20tenant%20it%20fails%20when%20hitting%20line%2044%20which%20has%20the%20command%20%24context.executequery().%26nbsp%3B%20Here's%20the%20error%20message%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EException%20calling%20%22ExecuteQuery%22%20with%20%220%22%20argument(s)%3A%20%22The%20sign-in%20name%20or%3CBR%20%2F%3Epassword%20does%20not%20match%20one%20in%20the%20Microsoft%20account%20system.%22%3CBR%20%2F%3EAt%20D%3A%5Cprojects%5Csandbox%5CSPO%20Powershell%5CSPOInventory.ps1%3A44%20char%3A4%3CBR%20%2F%3E%2B%20%24context.ExecuteQuery()%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20IdcrlException%3C%2FP%3E%3CP%3EThe%20collection%20has%20not%20been%20initialized.%20It%20has%20not%20been%20requested%20or%20the%3CBR%20%2F%3Erequest%20has%20not%20been%20executed.%20It%20may%20need%20to%20be%20explicitly%20requested.%3CBR%20%2F%3EAt%20D%3A%5Cprojects%5Csandbox%5CSPO%20Powershell%5CSPOInventory.ps1%3A46%20char%3A13%3CBR%20%2F%3E%2B%20ForEach%20(%24List%20in%20%24Lists)%3CBR%20%2F%3E%2B%20~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5B%5D%2C%20CollectionNotInitializ%3CBR%20%2F%3EedException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.SharePoint.Client.CollectionNotInitial%3CBR%20%2F%3EizedException%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20believe%20this%20is%20caused%20by%20the%20fact%20that%20in%20our%20test%20tenant%20we%20are%20using%20ADFS%2C%20but%20I'm%20not%20sure%20of%20how%20to%20fix%20the%20powershell%20script%20to%20make%20it%20work.%26nbsp%3B%20Here%20is%20my%20powershell%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdd-Type%20%E2%80%93Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5Cmicrosoft%20shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%3CBR%20%2F%3EAdd-Type%20%E2%80%93Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5Cmicrosoft%20shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%3C%2FP%3E%3CP%3E%24AdminUrl%20%3D%20%22tenant%20admin%20url%22%3CBR%20%2F%3E%24UserName%20%3D%20%22administrator%20account%22%3CBR%20%2F%3E%24Password%20%3D%20%22the%20password%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24SecurePassword%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%3CBR%20%2F%3E%24Credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-argumentlist%20%24userName%2C%20%24SecurePassword%3CBR%20%2F%3E%24SPOCredentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24UserName%2C%20%24SecurePassword)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20Get-SPOWebs()%7B%3CBR%20%2F%3Eparam(%3CBR%20%2F%3E%24Url%20%3D%20%24(throw%20%22Please%20provide%20a%20Site%20Collection%20Url%22)%2C%3CBR%20%2F%3E%24Credential%20%3D%20%24(throw%20%22Please%20provide%20a%20Credentials%22)%3CBR%20%2F%3E)%3CBR%20%2F%3E%3CBR%20%2F%3E%24context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24Url)%3CBR%20%2F%3E%24context.Credentials%20%3D%20%24SPOCredentials%3CBR%20%2F%3E%24web%20%3D%20%24context.Web%3CBR%20%2F%3E%24context.Load(%24web)%3CBR%20%2F%3E%24context.Load(%24web.Webs)%3CBR%20%2F%3E%24context.ExecuteQuery()%3CBR%20%2F%3Eforeach(%24web%20in%20%24web.Webs)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EGet-SPOWebs%20-Url%20%24web.Url%20-Credential%20%24Credential%3CBR%20%2F%3E%24web%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Efunction%20Get-ItemCount(%24Url)%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%24context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24Url)%3CBR%20%2F%3E%24context.Credentials%20%3D%20%24SPOCredentials%3CBR%20%2F%3E%24Lists%20%3D%20%24context.web.Lists%3CBR%20%2F%3E%24context.Load(%24Lists)%3CBR%20%2F%3E%24context.ExecuteQuery()%3CBR%20%2F%3E%24itemCount%20%3D%200%3CBR%20%2F%3EForEach%20(%24List%20in%20%24Lists)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24listTitle%20%3D%20%24List.Title%3CBR%20%2F%3EIf%20(%24listTitle%20-ne%20'User%20Information%20List'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Work%20Flow%20History'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Images'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Site%20Assets'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Composed%20Looks'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Microfeed'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Workflow%20Tasks'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Access%20Requests'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Master%20Page%20Gallery'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Web%20Part%20Gallery'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Style%20Library'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'List%20Template%20Library'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'appdata'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'appfiles'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Content%20type%20publishing%20error%20log'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Form%20Templates'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Converted%20Forms'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Solution%20Gallery'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'TaxonomyHiddenList'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Theme%20Gallery'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'List%20Template%20Gallery'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Maintenance%20Log%20Library'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Cache%20Profiles'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Web%20Template%20Extensions'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'Content%20and%20Structure%20Reports'%20-and%20%60%3CBR%20%2F%3E%24listTitle%20-ne%20'wfpub')%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24qry%20%3D%20%5BMicrosoft.SharePoint.Client.CamlQuery%5D%3A%3ACreateAllItemsQuery()%3CBR%20%2F%3E%24items%20%3D%20%24list.GetItems(%24qry)%3CBR%20%2F%3E%24Context.Load(%24items)%3CBR%20%2F%3E%24Context.ExecuteQuery()%3CBR%20%2F%3E%24itemCount%20%2B%3D%20%24items.count%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%20%24itemCount%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%23Retrieve%20all%20site%20collection%20infos%3CBR%20%2F%3E%24filePath%20%3D%20%22D%3A%5CProjects%5CSandbox%5CSPO%20Powershell%5Cspositeinventroy.csv%22%3CBR%20%2F%3E%24filePath2%20%3D%20%22D%3A%5CProjects%5CSandbox%5CSPO%20Powershell%5Cspoiteminventory.csv%22%3CBR%20%2F%3EConnect-SPOService%20-Url%20%24AdminUrl%3CBR%20%2F%3E%24sites%20%3D%20Get-SPOSite%3CBR%20%2F%3E%24row%20%3D%20%22Site%20Name%2CSite%20Path%2CSite%20Collection%20Path%2CIs%20Site%20Collection%2CItem%20Count%22%3CBR%20%2F%3EAdd-Content%20%24filePath%20%24row%3C%2FP%3E%3CP%3Eforeach%20(%24site%20in%20%24sites)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24itemCount%20%3D%200%3CBR%20%2F%3E%24siteCol%20%3D%20%24site.Url%3CBR%20%2F%3E%24itemCount%20%3D%20Get-ItemCount(%24siteCol)%3CBR%20%2F%3E%24row%20%3D%20%24site.Title%20%2B%20%22%2C%22%20%2B%20%24site.Url%20%2B%20%22%2C%22%20%2B%20%24siteCol%20%2B%20%22%2C%22%20%2B%20%22Y%22%20%2B%20%22%2C%22%20%2B%20%24itemCount%3CBR%20%2F%3EAdd-Content%20%24filePath%20%24row%3CBR%20%2F%3Ewrite-host%20%24site.Url%20%22%20Item%20Count%20%3D%20%22%20%24itemCount%3CBR%20%2F%3E%24AllWebs%20%3D%20Get-SPOWebs%20-Url%20%24site.Url%20-Credential%20%24SPOCredentials%3CBR%20%2F%3Eforeach(%24web%20in%20%24Allwebs)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24itemCount%20%3D%200%3CBR%20%2F%3E%24webURL%20%3D%20%24web.Url%3CBR%20%2F%3E%24itemCount%20%3D%20Get-ItemCount(%24webURL)%3CBR%20%2F%3E%24row%20%3D%20%24web.Title%20%2B%20%22%2C%22%20%2B%20%24web.Url%20%2B%20%22%2C%22%20%2B%24siteCol%20%2B%20%22%2C%22%20%2B%20%22N%22%20%2B%20%22%2C%22%20%2B%20%24itemCount%3CBR%20%2F%3EAdd-Content%20%24filePath%20%24row%3CBR%20%2F%3Ewrite-host%20%24web.Url%20%22%20Item%20Count%20%3D%20%22%20%24itemCount%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Elike%20I%20said%20above%20this%20works%20fine%20in%20my%20developer%20tenant%2C%20but%20fails%20in%20my%20test%20tenant.%26nbsp%3B%20I%20am%20certain%20that%20I%20am%20using%20the%20proper%20credentials%2C%20but%20there%20must%20be%20something%20that%20I%20am%20missing%20in%20order%20to%20have%20this%20work%20with%20ADFS.%26nbsp%3B%20Any%20help%20is%20appcreciated%20thank!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-354528%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20for%20SharePoint%20online%20inventory%20fails%20with%20FullyQualifiedErrorId%20%3A%20IdcrlExcept%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354528%22%20slang%3D%22en-US%22%3EDoes%20your%20admin%20account%20have%20permissions%20on%20the%20site%20collections%3F%20If%20not%2C%20are%20you%20able%20to%20create%20an%20onmicrosoft%20service%20account%20for%20that%20tenant%3F%3CBR%20%2F%3E%3CBR%20%2F%3EHave%20you%20thought%20about%20using%20the%20Graph%20API%20from%20PowerShell%20for%20getting%20SPO%20reports%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-354295%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20for%20SharePoint%20online%20inventory%20fails%20with%20FullyQualifiedErrorId%20%3A%20IdcrlExcept%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354295%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F210828%22%20target%3D%22_blank%22%3E%40Dave%20Walker%3C%2FA%3E%20%2C%20use%20SharePoint-pnp-cmdlets%20version%20for%20%3CSPAN%3ESharePoint%20Online%3C%2FSPAN%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fsharepoint%2Fsharepoint-pnp%2Fsharepoint-pnp-cmdlets%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fsharepoint%2Fsharepoint-pnp%2Fsharepoint-pnp-cmdlets%3Fview%3Dsharepoint-ps%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20try%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22hljs-pscommand%22%3EConnect-PnPOnline%3C%2FSPAN%3E%3CSPAN%20class%3D%22hljs-parameter%22%3E%20-Url%3C%2FSPAN%3E%3CSPAN%3E%20%3CA%20href%3D%22https%3A%2F%2Fyoursite.sharepoint.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fyoursite.sharepoint.com%3C%2FA%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22hljs-parameter%22%3E%20-UseWebLogin%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ECheers%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EFederico%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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?