microsoft / Partner-Center-PowerShell

PowerShell module for managing Partner Center resources.
https://docs.microsoft.com/powershell/partnercenter/
MIT License
132 stars 60 forks source link

Azure Functions: Assembly with same name is already loaded System.Management.Automation.dll #335

Open penicaudm opened 4 years ago

penicaudm commented 4 years ago

Steps to reproduce

Importing the module version 3.0.10 fails with errors.

ERROR: Assembly with same name is already loaded

Exception             : 
    Type       : System.IO.FileLoadException
    Message    : Assembly with same name is already loaded
    TargetSite : 
        Name          : Bind_LoadAssemblies
        DeclaringType : initialsessionstate
        MemberType    : Method
        Module        : System.Management.Automation.dll
    StackTrace : 
   at System.Management.Automation.Runspaces.InitialSessionState.Bind_LoadAssemblies(ExecutionContext context)
   at System.Management.Automation.Runspaces.InitialSessionState.Bind(ExecutionContext context, Boolean updateOnly, PSModuleInfo module, Boolean noClobber, Boolean local, Boolean setLocation)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
    Source     : System.Management.Automation
    HResult    : -2146232799
CategoryInfo          : InvalidOperation: (:) [Import-Module], FileLoadException
FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
InvocationInfo        : 
    MyCommand        : Import-Module
    ScriptLineNumber : 68
    OffsetInLine     : 1
    HistoryId        : 1
    ScriptName       : D:\home\site\wwwroot\profile.ps1
    Line             : Import-Module PartnerCenter 4>$null

    PositionMessage  : At D:\home\site\wwwroot\profile.ps1:68 char:1
                       + Import-Module PartnerCenter 4>$null
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : D:\home\site\wwwroot
    PSCommandPath    : D:\home\site\wwwroot\profile.ps1
    InvocationName   : Import-Module
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, D:\home\site\wwwroot\profile.ps1: line 68

Expected behavior

Module should load.

Actual behavior

Module fails with reported error. It started happening on the 28th of October.

Diagnostic logs

Environment

I did not change anything when the issue started appearing. Azure functions runtime version 3 PS 7.0

requirements.psd1

@{
    'Az' = '4.7'
    'PartnerCenter' = '3.*'
}

I have tried using Az 5.0 as well but this did not change anything. I am not sure if it is related to Az Powershell changes, as I did not see anything too specific about this. I could be wrong.

profile.ps1 imports module AzTable before importing Partner Center.

I will update if I find anything that could be used.

AusXTech commented 4 years ago

I have been having very similar issues since around the same time. None of my Azure functions that are using the partner centre PowerShell module are working due to whatever issue this is. I have tested code on my work Azure account as well as my personal one with the same results.

Are your Az modules importing? I'm also getting this error at the moment: The Function app may be missing a module containing the Connect-AzAccount command definition

In my troubleshooting, I've found the PartnerCenter module will only import when the system-assigned managed identity is disabled, when the managed identity is enabled the I get errors stating that the module could not be loaded.

As part of troubleshooting, I've been using the following run.ps1 code:

using namespace System.Net

param($Request, $TriggerMetadata)

Write-Host "PowerShell HTTP trigger function processed a request."

Get-PartnerCustomer

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
})

With the Requirements.psd:

@{

    'Az' = '5.*'
    'PartnerCenter' = '3.*'

}

Other than that its the default HTTP request template.

With system managed identity disabled, I get the error: [Error] ERROR: Run Connect-PartnerCenter to login.

With system managed identity enabled, I get the error: [Error] ERROR: The 'Get-PartnerCustomer' command was found in the module 'PartnerCenter', but the module could not be loaded. For more information, run 'Import-Module PartnerCenter'.

Running 'Import-Module PartnerCenter' doesn't change anything.

I also am getting the following errors with the managed identity enabled:

[Error] ERROR: The term 'Disable-AzContextAutosave' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[Error] ERROR: The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Edit:

Found a fix for the Az module errors: here Adding "'Az.Accounts' = '1.9.5'" my requirements.psd1 file fixed the issue.

penicaudm commented 4 years ago

Thanks @AusXTech as Az.Account is required for MSI the conflict seems to come from there, my function is successfully signing in using the MSI and getting Key Vault access. I'll try with the specific version of Az.Accounts and go from there.

slavizh commented 4 years ago

Same issue here. I have tried even in PowerShell 7.1

Import-Module -Name Az.Accounts -RequiredVersion 2.1.2
Import-Module -Name Az.Resources -RequiredVersion 3.0.0
Import-Module -Name Az.ManagementPartner -RequiredVersion 0.7.2
Import-Module -Name Az.Storage -RequiredVersion 3.0.0
Import-Module -Name Az.Monitor -RequiredVersion 2.2.0
Import-Module -Name PartnerCenter -RequiredVersion 3.0.10
Import-Module: Assembly with same name is already loaded
Exception             :
    Type       : System.IO.FileLoadException
    Message    : Assembly with same name is already loaded
    TargetSite :
        Name          : Bind_LoadAssemblies
        DeclaringType : initialsessionstate
        MemberType    : Method
        Module        : System.Management.Automation.dll
    StackTrace :
   at System.Management.Automation.Runspaces.InitialSessionState.Bind_LoadAssemblies(ExecutionContext context)
   at System.Management.Automation.Runspaces.InitialSessionState.Bind(ExecutionContext context, Boolean updateOnly, PSModuleInfo module, Boolean noClobber, Boolean local, Boolean setLocation)
   at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags manif
estProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
    Source     : System.Management.Automation
    HResult    : -2146232799
CategoryInfo          : InvalidOperation: (:) [Import-Module], FileLoadException
FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
InvocationInfo        :
    MyCommand        : Import-Module
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 13
    Line             : Import-Module -Name PartnerCenter -RequiredVersion 3.0.10
    PositionMessage  : At line:1 char:1
                       + Import-Module -Name PartnerCenter -RequiredVersion 3.0.10
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Import-Module
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
penicaudm commented 4 years ago

@slavizh I haven't been able to make it work with Az.Accounts 2.1.2 despite the fix mentioned here https://github.com/Azure/azure-functions-powershell-worker/issues/552

Trying today with 1.9.5, I might import it in the modules folder

slavizh commented 4 years ago

@penicaudm I am not even using functions but downgrading to 1.9.5 is not an option as actually I am trying to upgrade our scripts to Az 5.0.0 so if this is a problem I would just not upgrade at this time.

slavizh commented 4 years ago

This works fine if I try to import first the partner center module but it is not something I want to do as import it only when I have to use it.

Hipster74 commented 4 years ago

Ok, same here, partner center module broken in Azure functions, hours wasted. Any news on this issue?

I get:

2020-11-10T11:59:41.924336985Z: [INFO] 2020-11-10T11:59:41.924684085Z: [INFO] Exception : 2020-11-10T11:59:41.924706485Z: [INFO] Type : System.Management.Automation.CommandNotFoundException 2020-11-10T11:59:41.924711985Z: [INFO] ErrorRecord : 2020-11-10T11:59:41.924716285Z: [INFO] Exception : 2020-11-10T11:59:41.925030985Z: [INFO] Type : System.Management.Automation.ParentContainsErrorRecordException 2020-11-10T11:59:41.925043685Z: [INFO] Message : The 'New-PartnerAccessToken' command was found in the module 'PartnerCenter', but the module could not be loaded. For more information, run 'Import-Module PartnerCenter'. 2020-11-10T11:59:41.925048685Z: [INFO] HResult : -2146233087 2020-11-10T11:59:41.925302185Z: [INFO] TargetObject : New-PartnerAccessToken 2020-11-10T11:59:41.925314785Z: [INFO] CategoryInfo : ObjectNotFound: (New-PartnerAccessToken:String) [], ParentContainsErrorRecordException 2020-11-10T11:59:41.925319785Z: [INFO] FullyQualifiedErrorId : CouldNotAutoloadMatchingModule 2020-11-10T11:59:41.925517085Z: [INFO] InvocationInfo : 2020-11-10T11:59:41.925529385Z: [INFO] ScriptLineNumber : 99 2020-11-10T11:59:41.925533985Z: [INFO] OffsetInLine : 24 2020-11-10T11:59:41.925538285Z: [INFO] HistoryId : 1 2020-11-10T11:59:41.925542485Z: [INFO] ScriptName : /home/site/wwwroot/TimerTrigger1/run.ps1

penicaudm commented 4 years ago

I've not been able to fix it yet, even with Az.Accounts 1.9.5. I'll let you know if I manage to find a common version all the modules can load (1.9.5 of Az.Accounts gave me errors with AzTable, Az.Storage and Az.Resources...)

Hipster74 commented 4 years ago

@penicaudm Ok, thanks for the update, this is serious, hopefully someone on the Microsoft team can have a look soon.

slavizh commented 4 years ago

Just tested in PS 7.1 and issue is still present.

slavizh commented 4 years ago

The PowerShell Az team reported that the problems I have - the one specific to PS 7.1 and other that is not specific to PS version should be fixed by the Partner Center module team. Statement is located here: https://github.com/Azure/azure-powershell/issues/13423

slavizh commented 3 years ago

You can now load Partner Center 3.0.10 before loading Az.Accounts 2.2.1 but when you try to use New-PartnerAccessToken you will get error:

AADSTS9002313: Invalid request. Request is malformed or
     | invalid. Trace ID: 56dff39f-e523-456e-b2b2-46a073b78800
     | Correlation ID: 8ca604e3-ba2c-401a-8c7a-bcda1ed0fdef
     | Timestamp: 2020-11-18 14:01:44Z

This happens only on PS 7.1. Works on 6.2. OS Ubuntu 1804

MortenRei commented 3 years ago

Any news to this issue? Anyone been able to find a combination that actually works?