Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.11k stars 3.75k forks source link

Update-AzMaintenanceConfiguration always fail with Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' #25321

Open theheatDK opened 1 week ago

theheatDK commented 1 week ago

Description

Hi, I cannot find any way of getting Update-AzMaintenanceConfiguration to work. I am using Az 12.0.0.

Get-InstalledModule -Name Az -AllVersions | select Name,Version

Name Version
---- -------
Az   12.0.0
$MaintenanceConfig = Get-AzMaintenanceConfiguration -name 'aum-pelo01'
Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig

Here I get an existing configuration and send it to Update-AzMaintenanceConfiguration.

It always fail with: `Update-AzMaintenanceConfiguration: Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' to the type 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' required by parameter 'Configuration'. Specified method is not supported.

The same happens if I make a small change to a parameter:

$MaintenanceConfig = Get-AzMaintenanceConfiguration -name 'aum-pelo01'
$MaintenanceConfig[0].WindowParameterKbNumberToInclude += '123456'
Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig

Issue script & Debug output

PS C:\Users\pelo> $DebugPreference='Continue'
PS C:\Users\pelo> Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
Update-AzMaintenanceConfiguration: Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' to the type 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' required by parameter 'Configuration'. Specified method is not supported.

Environment data

PS C:\Users\pelo> $PSVersionTable

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

Module versions

PS C:\Users\pelo> Get-Module Az*

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     3.0.0                 Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script     1.4.1                 Az.Maintenance                      {Get-AzApplyUpdate, Get-AzConfigurationAssignment, Get-AzMaintenanceConfiguration, Get-Az…

Error output

PS C:\Users\pelo> $MaintenanceConfig = Get-AzMaintenanceConfiguration -name 'aum-pelo01'
PS C:\Users\pelo> $MaintenanceConfig[0].WindowParameterKbNumberToInclude += '123456'
PS C:\Users\pelo> Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
Update-AzMaintenanceConfiguration: Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' to the type 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' required by parameter 'Configuration'. Specified method is not supported.
PS C:\Users\pelo> Resolve-AzError

   HistoryId: 3

Message        : Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' to the type 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' required by
                 parameter 'Configuration'. Specified method is not supported.
StackTrace     :    at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collec
                 tionTypeInfo, Object currentValue)
                    at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
                    at System.Management.Automation.CmdletParameterBinderController.BindParameter(CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags
                 )
                    at System.Management.Automation.CmdletParameterBinderController.BindParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter, Param
                 eterBindingFlags flags)
                    at System.Management.Automation.CmdletParameterBinderController.BindNamedParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter)
                    at System.Management.Automation.ParameterBinderController.BindNamedParameters(UInt32 parameterSets, Collection`1 arguments)
                    at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)
                    at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
                    at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
                    at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)
                    at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
                    at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
                    at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                 --- End of stack trace from previous location ---
                    at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                    at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedi
                 rection[][] commandRedirections, FunctionContext funcContext)
                    at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Exception      : System.Management.Automation.ParameterBindingException
InvocationInfo : {Update-AzMaintenanceConfiguration}
Line           : Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
Position       : At line:1 char:100
                 + … Name 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
                 +                                                      ~~~~~~~~~~~~~~~~~~
HistoryId      : 3

Message        : Specified method is not supported.
StackTrace     :    at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collec
                 tionTypeInfo, Object currentValue)
Exception      : System.NotSupportedException
InvocationInfo : {Update-AzMaintenanceConfiguration}
Line           : Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
Position       : At line:1 char:100
                 + … Name 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
                 +                                                      ~~~~~~~~~~~~~~~~~~
HistoryId      : 3
microsoft-github-policy-service[bot] commented 1 week ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @adana-popescu, @KalpeshChavan12, @ralucaminea.

microsoft-github-policy-service[bot] commented 1 week ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @adana-popescu, @KalpeshChavan12, @ralucaminea.

theheatDK commented 2 days ago

Hi, I found the problem. If I call Get-AzMaintenanceConfiguration with an ResourceGroupName parameter it works, if I don't it fails.

Without the ResourceGroupName parameter is returns a List`1 and with a ResourceGroupName parameter is returns a PSMaintenanceConfiguration.


PS C:\> $MaintenanceConfig = Get-AzMaintenanceConfiguration -name 'aum-pelo01'
PS C:\> $MaintenanceConfig.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     List`1                                   System.Object

PS C:\> $MaintenanceConfig.count
1
PS C:\> Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig
Update-AzMaintenanceConfiguration: Cannot convert 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' to the type 'Microsoft.Azure.Commands.Maintenance.Models.PSMaintenanceConfiguration' required by parameter 'Configuration'. Specified method is not supported.
PS C:\>
PS C:\> $MaintenanceConfig = Get-AzMaintenanceConfiguration -name 'aum-pelo01' -ResourceGroupName 'pelo-test'
PS C:\> $MaintenanceConfig.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSMaintenanceConfiguration               System.Object

PS C:\> $MaintenanceConfig.count
1
PS C:\> Update-AzMaintenanceConfiguration -ResourceGroupName 'pelo-test' -Name 'aum-pelo01' -Configuration $MaintenanceConfig

Location                               : eastus2
Tags                                   : {"ContactPerson":"PELO","CostCenter":"11096040","MacoTask":"Nfrst"}
ExtensionProperties                    : {"InGuestPatchMode":"User"}
MaintenanceScope                       : InGuestPatch
Id                                     : /subscriptions/45fcd6a2-c6ba-4a15-b6ba-fcb3c93978aa/resourcegroups/pelo-test/providers/microsoft.maintenance/maintenanceconfigurations/aum-pelo
01
Name                                   : aum-pelo01
Type                                   : Microsoft.Maintenance/maintenanceConfigurations
StartDateTime                          : 2024-06-26 00:00
Duration                               : 03:55
Timezone                               : Romance Standard Time
Visibility                             : Custom
RecurEvery                             : 1Day
LinuxParameterClassificationToInclude  : System.Collections.Generic.HashSet`1[System.String]
LinuxParameterPackageNameMaskToExclude : System.Collections.Generic.HashSet`1[System.String]
LinuxParameterPackageNameMaskToInclude : System.Collections.Generic.HashSet`1[System.String]
WindowParameterKbNumberToInclude       : System.Collections.Generic.HashSet`1[System.String]
WindowParameterKbNumberToExclude       : System.Collections.Generic.HashSet`1[System.String]
WindowParameterClassificationToInclude : System.Collections.Generic.HashSet`1[System.String]
InstallPatchRebootSetting              : IfRequired```
isra-fel commented 2 days ago

That's probably it - source code

When there's no RG name the command lists all configurations, filter them, then put the results in a List then output. The solution is to call WriteObject() multiple times, each time writing a configuration object, or to use the other overload WriteObject (object sendToPipeline, bool enumerateCollection) to enumerate the list automatically.