Please help me,Workflow Runbook run error..

Dio Wang 21 Reputation points
2021-05-26T03:05:51.95+00:00

could you please tell me where I went wrong,my lead is anxious ?

workflow MyFirstRunbook-Workflow
{
Disable-AzContextAutosave -Scope Process

$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
$AppGw = Get-AzApplicationGateway -Name test01 -ResourceGroupName brain;
Stop-AzApplicationGateway -ApplicationGateway $AppGw;
}

I run this script, get the following error:
System.Management.Automation.ParameterBindingException: Cannot bind parameter 'ApplicationGateway'. Cannot convert value "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" to type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway". Error: "Cannot convert the "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" value of type "Deserialized.Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" to type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway"." ---> System.Management.Automation.PSInvalidCastException: Cannot convert value "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" to type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway". Error: "Cannot convert the "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" value of type "Deserialized.Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" to type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway"." ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" value of type "Deserialized.Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" to type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway".

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,198 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. tbgangav-MSFT 10,421 Reputation points
    2021-05-26T09:08:40.34+00:00

    Hi @Dio Wang ,

    I was also able to reproduce the error that you have provided. If we run the below 2 commands in any local machine then it works without any error but it fails when executed from an Azure Automation runbook. So, the issue is, in runbook $AppGw is holding the value of type "Deserialized.Microsoft.Azure.Commands.Network.Models.PSApplicationGateway" where as it's expected to hold the value of type "Microsoft.Azure.Commands.Network.Models.PSApplicationGateway". Hence the error.

    $AppGw = Get-AzApplicationGateway -Name test01 -ResourceGroupName brain;  
    Stop-AzApplicationGateway -ApplicationGateway $AppGw;  
    

    I will share this feedback internally with Azure Automation product and engineering team to fix the issue. However, as a workaround you may use the below runbook which is working without any issues when I have tested in my environment. Let me know if you face any issues while you try.

    workflow MyFirstRunbook-Workflow  
    {  
    Disable-AzContextAutosave -Scope Process  
      
    $Conn = Get-AutomationConnection -Name AzureRunAsConnection  
    Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint  
    Stop-AzApplicationGateway -ApplicationGateway (Get-AzApplicationGateway -Name test01 -ResourceGroupName brain)  
    }  
    

  2. wang jialu 51 Reputation points
    2021-05-27T08:03:45.53+00:00

    ,thank you very much,@tbgangav-MSFT
    I meet two questions,would you please give me a bit help :
    1:I find when I use foreach -parallel , all of them can stop ,Not all of them are running successfully,I tried to start it many times, but it still doesn't work;
    it said :Start-AzApplicationGateway : Long running operation failed with status 'Failed'. Additional Info:'An error occurred.' At StopStartAppGW:42 char:42 + + CategoryInfo : CloseError: (:) [Start-AzApplicationGateway], CloudException + FullyQualifiedErrorId :Microsoft.Azure.Commands.Network.StartAzureApplicationGatewayCommand

    2:I find when I use foreach -parallel , all of them can stop ,Not all of them are running successfully,I tried to start it many times, but it still doesn't work;
    it said :Start-AzApplicationGateway : Long running operation failed with status 'Failed'. Additional Info:'An error occurred.' At StopStartAppGW:42 char:42 + + CategoryInfo : CloseError: (:) [Start-AzApplicationGateway], CloudException + FullyQualifiedErrorId :Microsoft.Azure.Commands.Network.StartAzureApplicationGatewayCommand