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-AzVMRunCommand and Get-AzVmssVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM #22403

Closed
vivlingaiah opened this issue Jul 25, 2023 · 14 comments
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Compute - VM Compute - VMSS Service Attention This issue is responsible by Azure service team.

Comments

@vivlingaiah
Copy link
Contributor

vivlingaiah commented Jul 25, 2023

Description

-Get-AzVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM

  • Looks like this is a regression. This used to work

Issue script & Debug output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand

PS C:\> Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivVMSS -InstanceId 0
Get-AzVmssVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVmssVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmssVMRunCommand

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.2003
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22000.2003
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

10.0.1

Error output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand

PS C:\> Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivVMSS -InstanceId 0
Get-AzVmssVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVmssVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmssVMRunCommand
@vivlingaiah vivlingaiah added Compute 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 Jul 25, 2023
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Jul 25, 2023
@wyunchi-ms wyunchi-ms added Compute - VM Service Attention This issue is responsible by Azure service team. and removed Compute labels Jul 25, 2023
@ghost
Copy link

ghost commented Jul 25, 2023

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Drewm3, @TravisCragg-MSFT, @nikhilpatel909, @sandeepraichura, @hilaryw29, @GabstaMSFT.

Issue Details

Description

-Get-AzVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM

  • Looks like this is a regression. This used to work

Issue script & Debug output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.2003
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22000.2003
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

10.0.1

Error output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand
Author: vivlingaiah
Assignees: -
Labels:

Compute - VM, Service Attention, bug

Milestone: -

@vivlingaiah vivlingaiah changed the title Get-AzVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM Get-AzVMRunCommand and Get-AzVmssVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM Jul 25, 2023
@ghost
Copy link

ghost commented Jul 27, 2023

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @TravisCragg-MSFT, @sandeepraichura.

Issue Details

Description

-Get-AzVMRunCommand without -RunCommandName parameter fails to return list of RunCommands on VM

  • Looks like this is a regression. This used to work

Issue script & Debug output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand

PS C:\> Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivVMSS -InstanceId 0
Get-AzVmssVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVmssVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmssVMRunCommand

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22000.2003
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22000.2003
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

10.0.1

Error output

PS C:\> Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
Get-AzVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVMRunCommand -ResourceGroupName VivRG0 -VMName VivL
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand

PS C:\> Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivVMSS -InstanceId 0
Get-AzVmssVMRunCommand : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type
'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'.
At line:1 char:1
+ Get-AzVmssVMRunCommand -ResourceGroupName VivRG0 -VMScaleSetName VivV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzVmssVMRunCommand], InvalidCastException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmssVMRunCommand
Author: vivlingaiah
Assignees: -
Labels:

Compute - VM, Compute - VMSS, Service Attention, bug

Milestone: -

@Chris-Sanford
Copy link

Terminating existing PowerShell sessions and starting a new PowerShell process seems to be an effective workaround for this bug. Tested in the latest PowerShell Core and Az Module versions.

@TravisCragg-MSFT
Copy link

assigning to @nikhilpatel909

@grizzlytheodore
Copy link
Contributor

Hello from Compute PS team. We will address bug for September release. Thank you for submitting this bug item.

@nikhilpatel909
Copy link

@grizzlytheodore was this bug fixed?

@GabstaMSFT
Copy link

@grizzlytheodore checking if issue is now resolved for you?

@grizzlytheodore
Copy link
Contributor

@nikhilpatel909 @GabstaMSFT hello. this bug was resolved so I will close this issue

@RobertMochnacky
Copy link

RobertMochnacky commented Apr 3, 2024

Good morning,

I'm still experiencing the same issue to get the list of VM AzVMRunCommands without specifying RunCommandName parameter when the code runs as part of the powershell function in a function app. It works on occasional basis but not able to identify a pattern when it works and when not. The most of the time it fails with the following message:

Code:

param($Timer)

Set-AzContext -SubscriptionId $env:APPSETTING_SubscriptionId
$timeout = 30
$VMs = Get-AzVM -Status | Where-Object {$_.Tags['solution'] -eq 'SolutionName' -and $_.PowerState -eq "VM running"} # Get All Running VMs in the selected subscription which match solution:SolutionName Tag
Start-Sleep 5

if ($VMs -eq $null) {
    Write-Error "No VMs have been found. Please check whether the proper tag solution:SolutionName is present on the workload(s)"
    exit
}
foreach ($VM in $VMs) {
    Write-Information "Loading existing AzVMRunCommands for $($VM.Name)..."
    $existingCMDs = Get-AzVMRunCommand -ResourceGroupName $($VM.ResourceGroupName) -VMName $($VM.Name)
    if ($?) {
        Write-Information "Existing AzVMRunCommands for $($VM.Name) loaded sucessfuly."
    }
    else {
        Write-Error "Loading AzVMRunCommands for $($VM.Name) failed. Error: $Error[0].Exception.Message"
        exit
    }
}

===========================================================================================================================================================================

Output:

ERROR: Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type 'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'. Exception : Type : System.InvalidCastException TargetSite : Name : ChkCast_Helper DeclaringType : System.Runtime.CompilerServices.CastHelpers, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e MemberType : Method Module : System.Private.CoreLib.dll Message : Unable to cast object of type 'Microsoft.Azure.Management.Compute.Models.PSVirtualMachineRunCommand' to type 'Microsoft.Azure.Commands.Compute.Automation.Models.PSVirtualMachineRunCommandList'. Source : System.Private.CoreLib HResult : -2147467262 StackTrace : at lambda_method6013(Closure , VirtualMachineRunCommand , PSVirtualMachineRunCommandList , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source) at Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand.<ExecuteCmdlet>b__16_0() at Microsoft.Azure.Commands.Compute.ComputeClientBaseCmdlet.ExecuteClientAction(Action action) at Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand.ExecuteCmdlet() at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__31.b__3_0(T c) at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor) at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet) at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord() CategoryInfo : CloseError: (:) [Get-AzVMRunCommand], InvalidCastException FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.GetAzureVmRunCommand InvocationInfo : MyCommand : Get-AzVMRunCommand ScriptLineNumber : 18 OffsetInLine : 21 HistoryId : 1 ScriptName : C:\home\site\wwwroot\AzVmRunCommand_Cleanup\run.ps1 Line : $existingCMDs = Get-AzVMRunCommand -ResourceGroupName $($VM.ResourceGroupName) -VMName $($VM.Name) PositionMessage : At C:\home\site\wwwroot\AzVmRunCommand_Cleanup\run.ps1:18 char:21 + … stingCMDs = Get-AzVMRunCommand -ResourceGroupName $($VM.Resource … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PSScriptRoot : C:\home\site\wwwroot\AzVmRunCommand_Cleanup PSCommandPath : C:\home\site\wwwroot\AzVmRunCommand_Cleanup\run.ps1 InvocationName : Get-AzVMRunCommand CommandOrigin : Internal ScriptStackTrace : at , C:\home\site\wwwroot\AzVmRunCommand_Cleanup\run.ps1: line 18 PipelineIterationInfo :

`

Settings:

Function app settings:
PowerShell Core Version: 7.2
Platform: 32bit
Runtime version: ~4

Hosts.json:
{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}

requirements.psd1:
'Az' = '10.*'

Thank you in advance.

@grizzlytheodore
Copy link
Contributor

I am able to retrieve a list of RunCommands:
image

@grizzlytheodore
Copy link
Contributor

@RobertMochnacky, could you make sure you are running the latest version by running "update-module az" ?
then you will need to start a new powershell session to use the latest package

@RobertMochnacky
Copy link

Hello guys,
The command works well in the powershell session but the problem I reported occurs as part of the Powershell Function in a Function app.
I have also changed the module definition in the function app from 10.* to 11.* but still got the error mentioned earlier now and then.

@grizzlytheodore
Copy link
Contributor

@nikhilpatel909 could we get the above issue routed to ps functions?

@RobertMochnacky
Copy link

Addition based on the recent findings:
I have two PowerShell Functions:

  1. The function to find all VMs with as specific tag and Set the AzVMRunCommand for each VM, It waits 2 minutes or until the provisioning status of the Set-AzVMRunCommand is "Succeeded" then Remove AzVMRunCommand. This is schedule to run every 5 minutes. Each AzRUnCommand has its own name with start time tag in it. It sometimes happens that the removal of the command does not succeed and the AzVMRunCommand stays there. In order to avoid hitting the maximum nr. of AzVMRunCommand per VM (25) there is another function...

  2. The function finds the AzVMRunCommands for each VM and checks the timestamp in the AzVMRunCommand name. If it is older than 30 minutes, the run command gets deleted by the function.

The problem I've described in the previous posts only happens when the 1st and 2nd function is in the same Function App. As soon as I move the 2nd Function to a separate function app it works all the time.

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. Compute - VM Compute - VMSS Service Attention This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

8 participants