Azure PowerShell Get-AzVM returns an error

When using Get-AzVM an error about ‘System.Collections.Generic.IEnumerable’ is returned.

Symptoms

After upgrading to PowerShell 7.3.1 the Get-AzVM cmdlet is failing with an error, even when no parameters are supplied.

1Get-AzVM: GenericArguments[0], 'Microsoft.Azure.Management.Compute.Models.VirtualMachine', on 
2'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type 'T'.

The version (in my installation) of the cmdlet was 4.31.0. In PowerShell you can check the version of any cmdlet with Get-Command.

1Get-Command Get-AzVM
2
3CommandType     Name                                               Version    Source
4-----------     ----                                               -------    ------
5Cmdlet          Get-AzVM                                           4.31.0     Az.Compute

Solution

In my case this was fixed by updating the Azure PowerShell modules. You can do this with Update-Module as follows.

1Update-Module Az.*

Once this has completed open a new Powershell session and check the cmdlet has updated using Get-Command

1Get-Command Get-AzVM
2
3CommandType     Name                                               Version    Source
4-----------     ----                                               -------    ------
5Cmdlet          Get-AzVM                                           5.2.0      Az.Compute

Then check Get-AzVM and you should see it now works and returns the list of Azure Virtual Machines as expected.