Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.81k forks source link

Az.Functions should not require active Azure PowerShell session just to import the module. #23694

Closed techthoughts2 closed 4 months ago

techthoughts2 commented 9 months ago

Description

Issue

Importing the Az.Functions module fails if there is no active Azure PowerShell session:

Situation 1 - Az.Accounts is already imported

If Az.Accounts is already import into the session importing Az.Functions fails if there is no active Azure PowerShell session:

Import-Module Az.Functions

Import-Module: There is no active Azure PowerShell session. Please run 'Connect-AzAccount'

Situation 2 - Az.Accounts is not already imported

If Az.Accounts is not already import into the session importing Az.Functions fails stating it can not find the Get-AzContext command:

Import-Module Az.Functions

Get-AzContext: C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:1729:16
Line |
1729 |      $context = Get-AzContext -ErrorAction SilentlyContinue
     |                 ~~~~~~~~~~~~~
     | The term 'Get-AzContext' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Import-Module: There is no active Azure PowerShell session. Please run 'Connect-AzAccount'

Expected Result

Import-Module Az.Functions

# no errors on import

Context

This looks like it was introduced here: Migrate Functions from generation to main - #23277

Issue script & Debug output

DEBUG: [Stacks API] -  Build function stack definitions.
DEBUG: 5:34:49 AM - GetAzureRMContextCommand begin processing with ParameterSet 'GetSingleContext'.
DEBUG: 5:34:49 AM - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 5:34:49 AM - [ConfigManager] Got nothing from [DisplayRegionIdentified], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 5:34:49 AM - [ConfigManager] Got nothing from [CheckForUpgrade], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: AzureQoSEvent:  Module: Az.Accounts:2.13.2; CommandName: Get-AzContext; PSVersion: 7.3.3; IsSuccess: True; Duration: 00:00:00.0033514
DEBUG: 5:34:49 AM - [ConfigManager] Got [True] from [EnableDataCollection], Module = [], Cmdlet = [].
DEBUG: 5:34:49 AM - GetAzureRMContextCommand end processing.
Import-Module: There is no active Azure PowerShell session. Please run 'Connect-AzAccount'

Environment data

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.3
PSEdition                      Core
GitCommitId                    7.3.3
OS                             Microsoft Windows 10.0.20348
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

ModuleType Version Name
---------- ------- ----
    Script 2.13.2  Az.Accounts
    Script 2.9.1   Az.Accounts
    Script 0.2.0   Az.ADDomainServices
    Script 2.0.0   Az.Advisor
    Script 6.0.0   Az.Aks
    Script 0.1.0   Az.Alb
    Script 0.6.1   Az.AlertsManagement
    Script 1.1.4   Az.AnalysisServices
    Script 4.0.2   Az.ApiManagement
    Script 1.0.0   Az.App
    Script 1.3.0   Az.AppConfiguration
    Script 2.2.2   Az.ApplicationInsights
    Script 1.0.0   Az.ArcResourceBridge
    Script 2.0.0   Az.Attestation
    Script 1.0.0   Az.Automanage
    Script 1.9.1   Az.Automation
    Script 0.1.0   Az.BareMetal
    Script 3.5.0   Az.Batch
    Script 2.0.3   Az.Billing
    Script 0.1.0   Az.BillingBenefits
    Script 0.4.2   Az.Blueprint
    Script 0.5.0   Az.BotService
    Script 3.1.1   Az.Cdn
    Script 0.1.0   Az.ChangeAnalysis
    Script 2.0.0   Az.CloudService
    Script 1.14.1  Az.CognitiveServices
    Script 0.2.0   Az.Communication
    Script 7.1.0   Az.Compute
    Script 1.0.0   Az.ConfidentialLedger
    Script 0.2.0   Az.Confluent
    Script 0.10.0  Az.ConnectedKubernetes
    Script 0.5.2   Az.ConnectedMachine
    Script 0.1.0   Az.ConnectedNetwork
    Script 0.1.0   Az.ConnectedVMware
    Script 4.0.0   Az.ContainerInstance
    Script 4.1.2   Az.ContainerRegistry
    Script 1.13.0  Az.CosmosDB
    Script 0.3.1   Az.CostManagement
    Script 0.1.0   Az.CustomLocation
    Script 0.1.0   Az.CustomProviders
    Script 0.1.1   Az.Dashboard
    Script 0.3.0   Az.DataBox
    Script 1.1.0   Az.DataBoxEdge
    Script 1.7.1   Az.Databricks
    Script 0.1.0   Az.Datadog
    Script 1.18.0  Az.DataFactory
    Script 1.0.3   Az.DataLakeAnalytics
    Script 1.3.0   Az.DataLakeStore
    Script 0.14.3  Az.DataMigration
    Script 2.1.0   Az.DataProtection
    Script 1.0.1   Az.DataShare
    Script 0.3.0   Az.DedicatedHsm
    Script 1.1.0   Az.DeploymentManager
    Script 4.2.0   Az.DesktopVirtualization
    Script 1.0.0   Az.DevCenter
    Script 0.10.0  Az.DeviceProvisioningServices
    Script 0.1.0   Az.DeviceUpdate
    Script 0.7.4   Az.DevSpaces
    Script 1.0.2   Az.DevTestLabs
    Script 0.2.0   Az.DigitalTwins
    Script 0.3.0   Az.DiskPool
    Script 1.2.0   Az.Dns
    Script 0.2.1   Az.DnsResolver
    Script 0.1.0   Az.DynatraceObservability
    Script 0.1.0   Az.EdgeOrder
    Script 0.1.0   Az.Elastic
    Script 0.1.2   Az.ElasticSan
    Script 1.6.0   Az.EventGrid
    Script 4.2.0   Az.EventHub
    Script 0.1.0   Az.FluidRelay
    Script 1.10.0  Az.FrontDoor
    Script 4.0.7   Az.Functions
    Script 0.1.0   Az.GraphServices
    Script 0.11.0  Az.GuestConfiguration
    Script 0.3.0   Az.HanaOnAzure
    Script 6.0.2   Az.HDInsight
    Script 0.1.0   Az.HdInsightOnAks
    Script 0.1.0   Az.HealthBot
    Script 2.0.0   Az.HealthcareApis
    Script 0.1.1   Az.HPCCache
    Script 0.4.0   Az.ImageBuilder
    Script 0.2.0   Az.ImportExport
    Script 0.10.0  Az.IotCentral
    Script 2.7.5   Az.IotHub
    Script 5.0.1   Az.KeyVault
    Script 0.7.0   Az.KubernetesConfiguration
    Script 2.3.0   Az.Kusto
    Script 0.1.0   Az.LabServices
    Script 1.0.0   Az.LoadTesting
    Script 1.5.0   Az.LogicApp
    Script 0.1.0   Az.Logz
    Script 1.1.3   Az.MachineLearning
    Script 1.0.0   Az.MachineLearningServices
    Script 1.4.1   Az.Maintenance
    Script 1.2.0   Az.ManagedServiceIdentity
    Script 3.0.0   Az.ManagedServices
    Script 0.7.3   Az.ManagementPartner
    Script 0.8.0   Az.Maps
    Script 0.2.0   Az.MariaDb
    Script 0.3.0   Az.Marketplace
    Script 2.0.0   Az.MarketplaceOrdering
    Script 1.1.2   Az.Media
    Script 2.2.0   Az.Migrate
    Script 0.2.0   Az.MixedReality
    Script 0.3.0   Az.MobileNetwork
    Script 5.0.0   Az.Monitor
    Script 0.1.0   Az.MonitoringSolutions
    Script 1.1.1   Az.MySql
    Script 0.13.2  Az.NetAppFiles
    Script 7.1.0   Az.Network
    Script 0.1.0   Az.NetworkAnalytics
    Script 1.0.0   Az.NetworkCloud
    Script 0.1.2   Az.NetworkFunction
    Script 0.1.0   Az.NewRelic
    Script 0.1.0   Az.Nginx
    Script 1.1.2   Az.NotificationHubs
    Script 3.2.0   Az.OperationalInsights
    Script 0.1.1   Az.Orbital
    Script 0.2.0   Az.PaloAltoNetworks
    Script 0.4.0   Az.Peering
    Script 1.6.4   Az.PolicyInsights
    Script 0.1.0   Az.Portal
    Script 1.1.0   Az.PostgreSql
    Script 2.0.0   Az.PowerBIEmbedded
    Script 1.0.4   Az.PrivateDns
    Script 0.2.0   Az.ProviderHub
  Manifest 0.2.0   Az.Purview
    Script 0.1.0   Az.Quantum
    Script 0.1.0   Az.Qumulo
    Script 0.1.1   Az.Quota
    Script 6.6.2   Az.RecoveryServices
    Script 1.8.1   Az.RedisCache
    Script 1.2.0   Az.RedisEnterpriseCache
    Script 2.0.0   Az.Relay
    Script 0.12.0  Az.Reservations
    Script 0.13.0  Az.ResourceGraph
    Script 1.2.0   Az.ResourceMover
    Script 6.12.1  Az.Resources
    Script 0.10.0  Az.Search
    Script 1.5.1   Az.Security
    Script 3.1.1   Az.SecurityInsights
    Script 0.1.1   Az.SelfHelp
    Script 3.0.0   Az.ServiceBus
    Script 3.3.1   Az.ServiceFabric
    Script 0.2.0   Az.ServiceLinker
    Script 2.0.0   Az.SignalR
    Script 0.3.0   Az.SpringCloud
    Script 4.12.0  Az.Sql
    Script 2.1.0   Az.SqlVirtualMachine
    Script 0.2.0   Az.Ssh
    Script 0.1.0   Az.StackEdge
    Script 2.2.3   Az.StackHCI
    Script 0.1.0   Az.StackHCIVM
    Script 6.0.1   Az.Storage
    Script 0.1.0   Az.StorageCache
    Script 1.2.0   Az.StorageMover
    Script 2.1.0   Az.StorageSync
    Script 2.0.0   Az.StreamAnalytics
    Script 0.11.0  Az.Subscription
    Script 1.0.0   Az.Support
    Script 3.0.4   Az.Synapse
    Script 0.2.0   Az.TimeSeriesInsights
    Script 1.2.2   Az.TrafficManager
    Script 0.5.0   Az.VMware
    Script 0.1.1   Az.VoiceServices
    Script 3.1.2   Az.Websites
    Script 0.1.0   Az.WindowsIotServices
    Script 0.1.0   Az.Workloads

Error output

Resolve-AzError
DEBUG: 5:38:10 AM - ResolveError begin processing with ParameterSet 'AnyErrorParameterSet'.
DEBUG: 5:38:10 AM - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].

   HistoryId: -1

Message        : There is no active Azure PowerShell session. Please run 'Connect-AzAccount'
StackTrace     :    at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
Exception      : System.InvalidOperationException
InvocationInfo : {}
Line           :     throw $errorRecord

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:869 char:5
                 +     throw $errorRecord
                 +     ~~~~~~~~~~~~~~~~~~
HistoryId      : -1

Message        : There is no active Azure PowerShell session. Please run 'Connect-AzAccount'
StackTrace     :    at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
Exception      : System.InvalidOperationException
InvocationInfo : {}
Line           :     throw $errorRecord

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:869 char:5
                 +     throw $errorRecord
                 +     ~~~~~~~~~~~~~~~~~~
HistoryId      : -1

   HistoryId: 6

Message        : The term 'Get-AzContext' is not recognized as a name of a cmdlet, function, script file, or executable program.
                 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
StackTrace     : 
Exception      : System.Management.Automation.CommandNotFoundException
InvocationInfo : {Get-Command}
Line           : get-command Get-AzContext
Position       : At line:1 char:1
                 + get-command Get-AzContext
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 6

   HistoryId: -1

Message        : There is no active Azure PowerShell session. Please run 'Connect-AzAccount'
StackTrace     :    at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
Exception      : System.InvalidOperationException
InvocationInfo : {}
Line           :     throw $errorRecord

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:869 char:5
                 +     throw $errorRecord
                 +     ~~~~~~~~~~~~~~~~~~
HistoryId      : -1

   HistoryId: 5

Message        : The term 'Get-AzContext' is not recognized as a name of a cmdlet, function, script file, or executable program.
                 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
StackTrace     :    at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)
                    at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
                    at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
                    at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
                    at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Exception      : System.Management.Automation.CommandNotFoundException
InvocationInfo : {}
Line           :     $context = Get-AzContext -ErrorAction SilentlyContinue

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:1729 char:16
                 +     $context = Get-AzContext -ErrorAction SilentlyContinue
                 +                ~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'DEBUG_PREFIX'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'SetDefaultValueParameterWarningMessage'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'ReservedFunctionAppSettingNames'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'RuntimeToFormattedName'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'AllowedStorageTypes'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'RuntimeToDefaultOSType'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'RequiredStorageEndpoints'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

Message        : Cannot find a variable with the name 'DefaultFunctionsVersion'.
StackTrace     : 
Exception      : System.Management.Automation.ItemNotFoundException
InvocationInfo : {Get-Variable}
Line           :     if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue))

Position       : At C:\Users\*****\OneDrive\Documents\PowerShell\Modules\Az.Functions\4.0.7\custom\HelperFunctions.ps1:46 char:15
                 + …   if (-not (Get-Variable $variableName -ErrorAction SilentlyContinue) …
                 +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 5

DEBUG: 5:38:10 AM - [ConfigManager] Got nothing from [DisplayRegionIdentified], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 5:38:10 AM - [ConfigManager] Got nothing from [CheckForUpgrade], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: AzureQoSEvent:  Module: Az.Accounts:2.13.2; CommandName: Resolve-AzError; PSVersion: 7.3.3; IsSuccess: True; Duration: 00:00:00.2471316
DEBUG: 5:38:10 AM - [ConfigManager] Got [True] from [EnableDataCollection], Module = [], Cmdlet = [].
DEBUG: 5:38:10 AM - ResolveError end processing.
isra-fel commented 8 months ago

Thanks for the feedback. Let me route to the Azure Functions team

microsoft-github-policy-service[bot] commented 8 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AnatoliB, @Francisco-Gamino, @shreyabatra4.

microsoft-github-policy-service[bot] commented 8 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AnatoliB, @Francisco-Gamino, @shreyabatra4.

isra-fel commented 8 months ago

The Functions team is looking into this. For anyone blocked by the same issue, as a temporary work-around, please run Connect-AzAccount to login before importing the Az.Functions module or Az.

Francisco-Gamino commented 4 months ago

This issue is a duplicate of https://github.com/Azure/azure-powershell/issues/23788, which is fixed in Az 11.6.0.

/cc @dolauli @AnatoliB @andystaples @shreyabatra4