Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-AzContainerRegistry cmdlet throws error when $PSModuleAutoLoadingPreference is set to None #27014

Open
pewill opened this issue Jan 16, 2025 · 3 comments
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Container Registry customer-reported needs-team-attention This issue needs attention from Azure service team or SDK team

Comments

@pewill
Copy link

pewill commented Jan 16, 2025

Description

I want to explicitly load the modules I use and have disabled autoloading by setting $PSModuleAutoloadingPreference='None' in my profile. Most Az cmdlets works as expected but Az.ContainerRegistry doesn't play well with this approach.
When I run Get-AzContainerRegistry cmdlet it fails with error

ParentContainsErrorRecordException: <path to OneDrive>\Documents\PowerShell\Modules\Az.ContainerRegistry\4.3.0\ContainerRegistry.Autorest\exports\ProxyCmdletDefinitions.ps1:7226:9
Line |
7226 |          $steppablePipeline = $scriptCmd.GetSteppablePipeline($MyInvoc …
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "GetSteppablePipeline" with "1" argument(s): "The expression after '&' in a pipeline element produced an object that was not valid. It must result in a
     | command name, a script block, or a CommandInfo object."

Issue script & Debug output

# Easiest way to reproduce is to disable module autoloading in an existing session
# Assume Connect-AzAccount has been run
pwsh -NoProfile
Import-Module Az.ContainerRegistry
Get-AzContainerRegistry # works
$PSModuleAutoloadingPreference='None' 
Get-AzContainerRegistry # fails

Environment data

PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Microsoft Windows 10.0.26100
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    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     4.0.2                 Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script     4.3.0                 Az.ContainerRegistry                {Connect-AzContainerRegistry, Get-AzContainerRegistryManifest, Get-AzContainerRegistryRepository, Get-AzContainerRegistryTag…}

Error output

Message        : Exception calling "GetSteppablePipeline" with "1" argument(s): "The expression after '&' in a pipeline element produced an object that was not valid. It must re
                 sult in a command name, a script block, or a CommandInfo object."
StackTrace     :    at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                    at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown, ExecutionOptions options)
Exception      : System.Management.Automation.MethodInvocationException
InvocationInfo : {}
Line           :         $steppablePipeline = $scriptCmd.GetSteppablePipeline($MyInvocation.CommandOrigin)

Position       : At <path to OneDrive>\Documents\PowerShell\Modules\Az.ContainerRegistry\4.3.0\ContainerRegistry.Autorest\exports\ProxyCmdletDefinitions.ps1:7226 char:9
                 +         $steppablePipeline = $scriptCmd.GetSteppablePipeline($MyInvoc+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 4
@pewill pewill added bug This issue requires a change to an existing behavior in the product in order to be resolved. needs-triage This is a new issue that needs to be triaged to the appropriate team. labels Jan 16, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported needs-triage This is a new issue that needs to be triaged to the appropriate team. and removed needs-triage This is a new issue that needs to be triaged to the appropriate team. labels Jan 16, 2025
@isra-fel
Copy link
Member

Hi, does "path to onedrive" contain special char? Such as quotes? If yes, could you try installing the modules in a local directory?
Also, could you try enabling auto loading and run Az.ContainerRegistry cmdlets again? Want to check if they are related.
Thanks!

@isra-fel isra-fel added Container Registry needs-author-feedback More information is needed from author to address the issue. and removed needs-triage This is a new issue that needs to be triaged to the appropriate team. labels Jan 23, 2025
@pewill
Copy link
Author

pewill commented Jan 23, 2025

The path does not contain any special characters. I tested on two machines, one with a path containing a space and the other where no spaces are included in the path.
Toggling the $PSModuleAutoloadingPreference back to All makes it work again.

pwsh -NoProfile
Import-Module Az.ContainerRegistry
Get-AzContainerRegistry # works
$PSModuleAutoloadingPreference='None' 
Get-AzContainerRegistry # fails
$PSModuleAutoloadingPreference='All' 
Get-AzContainerRegistry # works again

Other cmdlets, like Get-AzContainerGroup from Az.ContainerInstance does not have this issue despite a similar implementation with a proxy cmdlet.

pwsh -NoProfile
Import-Module Az.ContainerInstance
Get-AzContainerGroup # works
$PSModuleAutoloadingPreference='None' 
Get-AzContainerGroup # Still works

The issue seems to be in the ProxyCmdletDefinitions.ps1 where the command is resolved using $cmdInfo = Get-Command -Name $mapping[$parameterSet]. When autoloading is disabled it will return $null instead of the name Get-AzContainerRegistry

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels Jan 23, 2025
@isra-fel
Copy link
Member

isra-fel commented Jan 24, 2025

Thanks for the research. It sure seems $PSModuleAutoLoadingPreference impacts whether Get-Command auto loads the module, as well as if it can return something. But this design of finding the correct parameter set is quite fundamental in the business logic that I'm not sure if we are able to change. @dolauli are there any alternative solutions that don't involve Get-Command?

Ref: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-7.4#:~:text=Get%2DCommand%20that%20uses%20the%20exact%20name%20of%20the%20command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Container Registry customer-reported needs-team-attention This issue needs attention from Azure service team or SDK team
Projects
None yet
Development

No branches or pull requests

2 participants