Forum Discussion
KathyROI
Aug 02, 2022Brass Contributor
Error 'Index was out of range. Must be non-negative and less than the size of the collection.'
I have version 2.0.6 of module ExchangeOnlineManagement installed and I am using WindowsPowerShell 5.1.
I issue the following connect statement inside a script:
Connect-ExchangeOnline -AppId <appid> -CertificateThumbprint <cert thumbprint> -Organization <organization>
And get the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
At HIDDEN\WindowsPowerShell\Modules\ExchangeOnlineManagement\2.0.6\netFramework\ExchangeOnlineManagement.psm1:750 char:21
+ throw $_.Exception.InnerException;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
When the script finishes running I issue the exact same connect statement and it executes fine. Anyone have any suggestions to get the connect statement to work the first time it's issued?
You can find a bit of running commentary about the issue here but since it's an error internal to the Connect-ExchangeOnline commandlet, there's little you can do about it directly yourself:
powershell - New-ExoPSSession: Index was out of range - Stack Overflow
I'd try the suggestion from the user "az1d" and explicitly load the module first before calling Connect-ExchangeOnline as that fits the description you've provided below where when you run the same command a second time, it works. This is because the first time you called it, it would have resulted in the implicit loading of the module first.
So, try this from a brand new PowerShell window where you haven't loaded or run anything yet:
Import-Module -Name ExchangeOnlineManagement; Connect-ExchangeOnline -Organization <blah> -AppId <blee> -CertificateThumbprint <blech>;
Cheers,
Lain
- LainRobertsonSilver Contributor
You can find a bit of running commentary about the issue here but since it's an error internal to the Connect-ExchangeOnline commandlet, there's little you can do about it directly yourself:
powershell - New-ExoPSSession: Index was out of range - Stack Overflow
I'd try the suggestion from the user "az1d" and explicitly load the module first before calling Connect-ExchangeOnline as that fits the description you've provided below where when you run the same command a second time, it works. This is because the first time you called it, it would have resulted in the implicit loading of the module first.
So, try this from a brand new PowerShell window where you haven't loaded or run anything yet:
Import-Module -Name ExchangeOnlineManagement; Connect-ExchangeOnline -Organization <blah> -AppId <blee> -CertificateThumbprint <blech>;
Cheers,
Lain
- KathyROIBrass Contributor
Hi LainRobertson ,
I do have the Import-Module statement preceding the connect statement in my script. The article you pointed me too suggests that you should NOT execute the Import statement so I will give that a try!
Uninstall 2.0.6 version isn't working for me. Can I simply delete the 2.0.6 folder to uninstall that version? I might just keep it as someone suggested in that other article the out of range issue was introduced in 2.0.4.
- LainRobertsonSilver Contributor
Hey, Kathy.
Good catch on the Stackoverflow article - I need my glasses checked. (Or just read things properly.)
I'm not sure about deleting the files as I've never had an issue removing a module. What error did you get?
If you run the following and Path begins with "C:\Program Files\WindowsPowerShell\Modules\" then you will need to run Uninstall-Module with local admin rights.
(Get-Module -Name ExchangeOnlineManagement).Path
Purely for comparison, I've been on 2.0.4 for a little while now and not had this error. That said, I only have the need to run it manually and always run the Import-Module first (just a habit I have.)
Cheers,
Lain
- LainRobertsonSilver Contributor
Below is an officially-logged discussion of the same issue, though the author doesn't provide the module version they hit the issue on and the discussion itself isn't particularly informative. It's only use is confirmation of the bug.
I probably should have also suggested updating to the current module version, 2.0.5, and removing 2.0.3 if that version's not explicitly needed.
Cheers,
Lain