SOLVED

Error 'Index was out of range. Must be non-negative and less than the size of the collection.'

Brass Contributor

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?

10 Replies
best response confirmed by KathyROI (Brass Contributor)
Solution

@KathyROI 

 

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

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.

 

Connect-ExchangeOnline fails with ArgumentOutOfRangeException · Issue #8222 · MicrosoftDocs/office-d...

 

Cheers,

Lain

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.

@KathyROI 

 

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

Hi @LainRobertson ,

I am running WindowsPowerShell as admin. When I run:

uninstall-module -name exchangeonlinemanagement -force

I get the following error:

PackageManagement\Uninstall-Package : The module 'ExchangeOnlineManagement' of version '2.0.6-Preview6' in module
base folder 'HIDDEN\PowerShell\Modules\ExchangeOnlineManagement\2.0.6' was
installed without side-by-side version support. Some versions are installed in this module base with side-by-side
version support. Uninstall other versions of this module before uninstalling the most current version.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2194 char:21
+ ... $null = PackageManagement\Uninstall-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-P
ackage], Exception
+ FullyQualifiedErrorId : UnableToUninstallModuleVersion,Uninstall-Package,Microsoft.PowerShell.PackageManage
ment.Cmdlets.UninstallPackage

I also tried to uninstall in PowerShell7 running as admin. It appears like it worked but it is still installed.

 

I have been running PowerShell7 on and off with my scripts to discover what does and does not work. Not quite prime time for me yet!

And I forgot to mention that removing the Import did seem to fix the issue!

Doesn't really make sense to me, thought most of the time you need to import the module before using commands. I'll just keep version 6 installed.

@KathyROI 

 

Yeah, I'm with you on the "doesn't make sense" part.

 

The Uninstall-Module error seems to indicate that the earlier versions of the module should be removed first. For the sake of simplicity, I tend to just "wipe the slate clean" using -AllVersions (I can be lazy like that), and then use Install-Module to put only the latest version back.

 

Here's some more in-depth discussion around pre-release modules as well as some specific content under "Examples" outlining how "side-by-side" support is intended to work.

 

Prerelease Module Versions - PowerShell | Microsoft Docs

 

Cheers,

Lain

Hi @LainRobertson 

I tried the -AllVersions to with no success. I couldn't even find evidence of other version being installed either. This is what I see in the ExchangeOnlineManagement folder:

KathyROI_0-1659706692536.png

 

@KathyROI 

 

If the "proper" ways just aren't working then perhaps try removing the module folder.

 

First, use the following command to check where it may be installed. I've installed 2.0.5 using the CurrentUser scope purely to demonstrate that modules can live in separate areas.

 

(Get-Module -ListAvailable -Name ExchangeOnlineManagement).Path

 

Which for me, produces:

LainRobertson_0-1659745969951.png

 

So, in my case, I'd want to remove the folders at:

 

  • C:\Users\lain.robertson\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement
  • C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement

 

I'm not sure what that green status circle means as my only point of reference is from the "by-gone" era of offline files, but I do get suspicious when I see it as the offline folders cache was a frequent troublemaker for users. But I'll assume that's not what it is and leave it at that.

 

Cheers,

Lain

Hi @LainRobertson ,

I figured this one out. I used the 'CurrentUser' option when installing so it installed the module in my personal OneDrive (which I don't want!). So I updated my installation to use AllUsers for all my modules. My other computer had multiple versions installed so I cleaned that up too. Seems those changes did the trick.

 

Thanks for all your suggestions and help!

1 best response

Accepted Solutions
best response confirmed by KathyROI (Brass Contributor)
Solution

@KathyROI 

 

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

View solution in original post