Call a script or function with #Requires -Modules [module info], while having the the required module installed. I've only tested with the PartnerCenter module, using either of these in my tests:
#Requires -Modules @{ModuleName='PartnerCenter';ModuleVersion='3.0.10'}, Az.Accounts
or
#Requires -Modules PartnerCenter
Both produce the same error when calling the script.
PS C:\Users\MY-USER> .\Documents\GitHub\PowerShell\Connect-PartnerCenterAsSecureApp.ps1
Import-Module: Assembly with same name is already loaded
The script I'm calling is very basic (https://github.com/JeremyTBradshaw/PowerShell/blob/master/Connect-PartnerCenterAsSecureApp.ps1)
I expect the script to not fail immediately and instead carry on because I have the module that is required already installed, and it shouldn't matter if it is already loaded or not. For this example script, I should get prompted for the values of the mandatory properties whereas my example has omitted using any parameters.
PS C:\Users\MY-USER>.\Documents\GitHub\PowerShell\Connect-PartnerCenterAsSecureApp.ps1
cmdlet Connect-PartnerCenterAsSecureApp.ps1 at command pipeline position 1
Supply values for the following parameters:
CSPTenantId:
PS C:\Users\MY-USER> .\Documents\GitHub\PowerShell\Connect-PartnerCenterAsSecureApp.ps1
Import-Module: Assembly with same name is already loaded
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.19041
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
This sounds like it might be an issue with the module.
Are you able to import that module outside of this context?
I see PartnerCenter depends on NewtonSoft.Json - perhaps Az module too but another version.
https://github.com/microsoft/Partner-Center-PowerShell/tree/master/src/lib/NetFxPreloadAssemblies
GitHub
PowerShell module for managing Partner Center resources. - microsoft/Partner-Center-PowerShell
@vexx32 I can import the module, as well as install and update, successfully. One thing that may be part of the issue is that I always install modules into CurrentUser scope as I'm working from a few computers without admin rights so I started that habit a while back for that reason. Below I'm trying to demonstrate how I can import the module and it successfully imports the latest version:
PowerShell 7.0.3
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/powershell
Type 'help' to get help.
PS C:\Users\MyUser> Get-Module PartnerCenter
PS C:\Users\MyUser> Import-Module PartnerCenter
PS C:\Users\MyUser> Get-Module PartnerCenter
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 3.0.10 PartnerCenter {Add-PartnerCustomerCartLineItem, Add-PartnerCustomerUserRoleMember,…
PS C:\Users\MyUser> Get-Module PartnerCenter -ListAvailable
Directory: C:\Users\MyUser\Documents\PowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 3.0.10 PartnerCenter Core,Desk {Add-PartnerCustomerCartLineItem, Add-PartnerCustomerUserR…
Script 3.0.9 PartnerCenter Core,Desk {Add-PartnerCustomerCartLineItem, Add-PartnerCustomerUserR…
PS C:\Users\MyUser>
Can you load both modules in the same session?
Well I had to be that guy, but it seems I was having trouble commenting out the line:
#Requires -Modules @{ModuleName='PartnerCenter';ModuleVersion='3.0.10'}, Az.Accounts
In the end, the problem is with the Az.Accounts module, and I get the same error when I manually try to import it.
PS C:\Users\MyUser> Get-module az.accounts
PS C:\Users\MyUser> Get-module az.accounts -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 1.5.2 Az.Accounts Core,Desk {Disable-AzDataCollection, Disable-AzContextAutosave, Enab…
PS C:\Users\MyUser> Import-Module az.Accounts
Import-Module: Assembly with same name is already loaded
PS C:\Users\MyUser>
I will continue to troubleshoot it now and report back anything I can find.
@iSazonov mentioned that both the Az.Accounts module and PartnerCenter depend on Newtonsoft.Json but seem to require different versions; the best bet would be to try to find a common version that can work for both and update one of the module manifests, I think.
@vexx32 and @iSazonov thanks again for you help. I just uninstalled the older 1.5.2 version of Az.Accounts (from the AllUsers scope) and installed the latest into CurrentUser. This immediately solved my problem.
I wish I had caught this before opening the issue, so apologies for the wasted cycles.
Most helpful comment
@vexx32 and @iSazonov thanks again for you help. I just uninstalled the older 1.5.2 version of Az.Accounts (from the AllUsers scope) and installed the latest into CurrentUser. This immediately solved my problem.
I wish I had caught this before opening the issue, so apologies for the wasted cycles.