none
Cannot create a subnet within a Workflow Runbook RRS feed

  • Question

  • Just started trying to implement a deployment script for our application to improve our devops and have hit a major stumbling block almost immediately.  The Automation Account is created and by default includes the the AzureRM modules, I read that these are now deprecated in favour of the Az modules.  I have added the following Az modules to a brand new Automation Account service: -

    1. Az.Automation
    2. Az.Compute
    3. Az.Storage
    4. Az.Profile
    5. Az.Resources
    6. Az.Sql
    7. Az.Network

    My child runbook is called from a parent runbook and is attempting to create a new Resource Group and VNet with a default subnet contained within it, all looked very simple within the Microsoft documentation, my runbook is: -

    workflow Build-AuditHub-VirtualNetwork
    {
        Param
        (
            [Parameter (Mandatory= $true)]
            [String]$resourceGroupName,
            [Parameter (Mandatory= $true)]
            [String]$virtualNetworkName
        )
    
        [OutputType(Boolean)]
        $success = $true    
    
        # Create the Resource Group to hold the Audit-Hub instance
    
        $resourceGroup=Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
    
        if($resourceGroup -ne $null)
        {
            Write-Verbose "The Resource Group $resourceGroupName supplied already exists within this subscription" -verbose
            $success = $false
        } else {
            $region = Get-AutomationVariable -Name 'Region'
            $resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $region
    
            Write-Verbose "Resource Group created with name: $resourceGroupName" -verbose
    
            # Create the Virtual Network
            try
            {
                $virtualNetwork = New-AzVirtualNetwork `
                    -ResourceGroupName $resourceGroupName `
                    -Location $region `
                    -Name $virtualNetworkName `
                    -AddressPrefix 10.2.0.0/24 `
                    -ErrorAction Stop
    
                Add-AzVirtualNetworkSubnetConfig `
                    -Name default `
                    -AddressPrefix 10.2.0.0/24 `
                    -VirtualNetwork $virtualNetwork `
                    -ErrorAction Stop
    
                $virtualNetwork | Set-AzVirtualNetwork -ErrorAction Stop
    
                Write-Verbose "Virtual Network created with name: $virtualNetworkName" -verbose
            }
            catch
            {
                Write-Verbose "Failed to create the Virtual Network with name: $virtualNetworkName" -verbose
                $success = $false
            }
        }
    
        $success        
    }
    When run I get the following error


    Add-AzVirtualNetworkSubnetConfig : Cannot bind parameter 'VirtualNetwork'. Cannot convert value 
    "Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork" to type 
    "Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork". Error: "Cannot convert the 
    "Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork" value of type 
    "Deserialized.Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork" to type 
    "Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork"."
    At Build-AuditHub-VirtualNetwork:38 char:38

        + CategoryInfo          : InvalidArgument: (:) [Add-AzVirtualNetworkSubnetConfig], ParameterBindingException
        + FullyQualifiedErrorId : 
    CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.Network.AddAzureVirtualNetworkSubnetConfigCommand

    If I manualy run the commands through the Azure Cloud Shell everything works fine, so I can't help but think its an issue with my setup within the Automation Account and specifically my modules but cannot work out what the problem is!


    • Edited by Andy HB Monday, August 19, 2019 2:10 PM
    Monday, August 19, 2019 2:09 PM

Answers

  • Hi,

    It is a long story of how cmdlets work Workflow runbook but it would be easier if you use PowerShell Script instead of workflow. Workflow is old concept that is not even present in PowerShell 6 for example. I recommend to always use script when you use Azure Automation.

    Mark this reply as answer if it has helped you.

    Tuesday, August 20, 2019 5:21 AM

All replies

  • Hi,

    It is a long story of how cmdlets work Workflow runbook but it would be easier if you use PowerShell Script instead of workflow. Workflow is old concept that is not even present in PowerShell 6 for example. I recommend to always use script when you use Azure Automation.

    Mark this reply as answer if it has helped you.

    Tuesday, August 20, 2019 5:21 AM
  • Thank you for your help I have now migrated everything to normal PS scripts and they work fine.
    Tuesday, August 20, 2019 10:05 AM