Create New Vnet In Azure

Hi,

Apologies, but I am pretty new to Powershell and creating Vnet’s in Azure. I am running the following command in PowerShell to try and create a new Vnet in my test Azure subscription (I have already logged in by running the command - Connect-AzAccount) and get the the error at the bottom -

New-AzVirtualNetwork -location “" -ResourceGroupName “” -TemplateFile "C:\Temp*.json" -TemplateParametersFile "C:\Temp****.json”

The error message I receive is this -

New-AzVirtualNetwork : A parameter cannot be found that matches parameter name ‘TemplateFile’.
At line:1 char:87

  • … th" -ResourceGroupName “****” -TemplateFile "C:\Tem …
  • CategoryInfo : InvalidArgument: (:slight_smile: [New-AzVirtualNetwork], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.Network.NewAzureVirtualNetworkCommand

Obviously the ****'s are my blanked out names.

Any help would be greatly appreciated.

Thanks
Steve

Hi,

Apologies, but I am pretty new to Powershell and creating Vnet's in Azure. I am running the following command in PowerShell to try and create a new Vnet in my test Azure subscription (I have already logged in by running the command – Connect-AzAccount) and get the the error at the bottom –

New-AzVirtualNetwork -location "***" -ResourceGroupName “******” -TemplateFile "C:\Temp\******.json" -TemplateParametersFile "C:\Temp\******.json"

The error message I receive is this –

New-AzVirtualNetwork : A parameter cannot be found that matches parameter name 'TemplateFile'.
At line:1 char:87
+ ... th" -ResourceGroupName “****” -TemplateFile "C:\Tem ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-AzVirtualNetwork], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.Network.NewAzureVirtualNetworkCommand

Obviously the ****'s are my blanked out names.

Any help would be greatly appreciated.

Thanks
Steve

Looking at the docs for New-AzVirtualNetwork, there are no such parameters named TemplateFile or TemplateParametersFile.

Can you share the contents of the json files with us? How did you generate them?

Hi,

Thank you for your response. I was starting to wonder if the command -templatefile just does not work alongside New-AzVirtualNetwork.

I generated the JSON files using some examples off github. I have pasted them below. JSON file -

{
“$schema”: “https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“vnetName”: {
“type”: “string”,
“metadata”: {
“description”: “VNet name”
}
},
“vnetAddressPrefix”: {
“type”: “string”,
“metadata”: {
“description”: “Address prefix”
}
},
“subnet1Prefix”: {
“type”: “string”,
“metadata”: {
“description”: “Subnet 1 Prefix”
}
},
“subnet1Name”: {
“type”: “string”,
“metadata”: {
“description”: “Subnet 1 Name”
}
},
“location”: {
“type”: “string”,
“metadata”: {
“description”: “Location for all resources.”
}
}
},
“variables”: {},
“resources”: [
{
“apiVersion”: “2018-10-01”,
“type”: “Microsoft.Network/virtualNetworks”,
“name”: “[parameters(‘vnetName’)]”,
“location”: “[parameters(‘location’)]”,
“properties”: {
“addressSpace”: {
“addressPrefixes”: [
“[parameters(‘vnetAddressPrefix’)]”
]
}
},
“resources”: [
{
“apiVersion”: “2018-10-01”,
“type”: “subnets”,
“location”: “[parameters(‘location’)]”,
“name”: “[parameters(‘subnet1Name’)]”,
“dependsOn”: [
“[parameters(‘vnetName’)]”
],
“properties”: {
“addressPrefix”: “[parameters(‘subnet1Prefix’)]”
}
}
]
}
]
}

JSON parameter file -

{
“$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“vnetName”:{
“value”: “******”
},

“vnetAddressPrefix”:{
“value”: “*******”
},

“subnet1Prefix”:{
“value”: “********”
},

“subnet1Name”:{
“value”: “********”
},

“location”:{
“value”: “********”
}
}
}

Basically what I am trying to achieve is to create a Vnet and then create a Web app all using JSON files (JSON file and a JSON parameter file)

Thank you in advance for your help.

Regards
Steve

The JSON content is an Azure Resource Manager schema.

I believe you shouldn’t be using the New-AzVirtualNetwork cmdlet, but rather the New-AzResourceGroupDeployment or New-AzDeployment cmdlet, combined with the -TemplateFile parameter. The content of the json already specify it is about deploying a Vnet.

Have a look at this documentation: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-deploy

That should help you out to accomplish your goal.

 

Thanks very much John I will give that a go and let you know if it works, thanks again.

I have now changed both the JSON file and JSON parameter file and I am still not able to deploy the Vnet. The error I am receiving is -

New-AzResourceGroupDeployment : 11:10:09 - Error: Code=InvalidTemplateDeployment; Message=The template deployment ‘*****’ is not valid according to the
validation procedure. The tracking id is ‘e8417530-266a-4c63-8c77-ef18741a4a32’. See inner errors for details. Please see https://aka.ms/arm-deploy for usage
details.

Any idea why the template is not showing as valid. I can share my changes if that would help.

Thanks
Steve

 

So I have made some further progress, but I have come across another issue. When I run the PowerShell command and input the following parameters I get an error -

nameFromTemplate: *********
resourceGroupFromTemplate: *********

New-AzResourceGroupDeployment : 19:43:33 - Resource Microsoft.Network/virtualNetworks ‘’ failed with message '{
“error”: {
“code”: “NetcfgInvalidSubnet”,
“message”: "Subnet '
’ is not valid in virtual network '***’.",
“details”:

Any help would be greatly appreciated. The code I am using is as follows -

JSON Script -

{
“$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“name”: {
“type”: “string”
},
“resourceGroup”: {
“type”: “string”
},
“location”: {
“type”: “string”
},
“addressPrefix”: {
“type”: “string”
},
“subnetName”: {
“type”: “string”
},
“subnetAddressPrefix”: {
“type”: “string”
},
“enableDdosProtection”: {
“type”: “bool”
}
},
“resources”: [
{
“apiVersion”: “2018-08-01”,
“name”: “[parameters(‘name’)]”,
“type”: “Microsoft.Network/virtualNetworks”,
“location”: “[parameters(‘location’)]”,
“properties”: {
“addressSpace”: {
“addressPrefixes”: [
“[parameters(‘addressPrefix’)]”
]
},
“subnets”: [
{
“name”: “[parameters(‘subnetName’)]”,
“properties”: {
“addressPrefix”: “[parameters(‘subnetAddressPrefix’)]”
}
}
],
“enableDdosProtection”: “[parameters(‘enableDdosProtection’)]”
}
}
]
}

JSON parameters file -

{
“$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“name”:{
“value”: “*****”
},

“resourceGroup”:{
“value”: “*********”
},

“location”:{
“value”: “********”
},

“addressPrefix”:{
“value”: “********”
},

“subnetName”:{
“value”: “***********”
},

“subnetAddressPrefix”:{
“value”: “*********”
},

“enableDdosProtection”:{
“value”: true
}
}
}

Thanks
Steve

Hi,

The parameters specified in the template.json need to be passed into the the deployment, either by hard-coding (not recommended) in the parameters.json or by passing in via the New-AzResourceGroupDeployment cmdlet and parameters

New-AzResourceGroupDeployment -Name 'VirtualNetworkTemplate' `
                              -ResourceGroupName 'NetworkResourceGroup' ` #This needs to already exist.
                              -TemplateParameterFile .\Parameters.json `
                              -TemplateFile .\Template.json `
                              -Location 'West Europe' `
                              -addressPrefix '10.10.10.10/24'
and so on.
Alternatively you can use the command
New-AzVirtualNetwork
and pass through the required parameters

Hope that makes sense

Hi,

Thank you for your response, that does kind of make sense. I see that you do not recommend putting the parameters in a parameter.json file, why is that? I am trying to create a Vnet purely using a Powershell script calling a JSON file.

I have crossed checked the fields specified in the template.json file and the parameter.json file and they both match up.

In Powershell when I am running the following command -

New-AzResourceGroupDeployment -location "" -ResourceGroupName “” -TemplateFile “C:\Temp\CreateNewVnet*" -TemplateParameterFile "C:\Temp\CreateNewVnet*

I am still getting prompted for the following fields -

nameFromTemplate:
resourceGroupFromTemplate:

I do not understand why I am getting prompted for these fields where as far as I am concerned those fields are specified in the template and parameter file.

I am inputting what I think are the correct fields and I am still receiving an error message -

New-AzResourceGroupDeployment : 13:34:52 - Resource Microsoft.Network/virtualNetworks ‘name’ failed with message '{
“error”: {
“code”: “NetcfgInvalidSubnet”,
“message”: “Subnet ‘*’ is not valid in virtual network '’.”,
“details”:

Any help would be greatly appreciated.

Thanks
Steve

 

Hi,

Can anyone help with this?

Thanks
Steve

 

Without knowing what arguments you’re giving the parameters, I couldn’t say why you’re getting an err about the subnet not being valid but I would guess that you’re either not using proper CIDR notation or you provided a subnet address space that doesn’t jive with the vnet address space.

Here’s a valid template, from your example above. Notice I’ve changed the name parameter to vnetname. This is because New-AzResourceGroupDeployment already has a required parameter called name. You’ll trip it up when your template uses parameters with the same names as the cmdlet.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vnetname": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "addressPrefix": {
            "type": "string"
        },
        "subnetName": {
            "type": "string"
        },
        "subnetAddressPrefix": {
            "type": "string"
        }
    },
    "resources": [
        {
            "apiVersion": "2018-08-01",
            "name": "[parameters('vnetname')]",
            "type": "Microsoft.Network/virtualNetworks",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('addressPrefix')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[parameters('subnetName')]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetAddressPrefix')]"
                        }
                    }
                ]
            }
        }
    ]
}

Here’s the parameter template. Name has been changed to vnetname here as well. Take a look at how the address spaces are notated. This is CIDR notation and it’s what Azure requires.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vnetname": {
            "value": "myVnet"
        },
        "location": {
            "value": "WestUS"
        },
        "addressPrefix": {
            "value": "10.10.0.0/16"
        },
        "subnetName": {
            "value": "mySubnet"
        },
        "subnetAddressPrefix": {
            "value": "10.10.1.0/24"
        }
    }
}

I removed the DDOS protection stuff for the sake of simplicity since that’s an entirely new subresource that needs to be built. Also removed the resource group parameters since a resource group deployment will specify this with the cmdlet; it’s not needed in the template.

You then deploy these together like so, New-AzResourceGroupDeployment -ResourceGroupName myresourcegroup -TemplateFile .\vnet.json -TemplateParameterFile .\vnet.parameters.json -Name mydeployment -Verbose

When learning ARM templates, start small and simple, get it working, and then build up in complexity.

You should also take a look at the vnet template reference docs, here.

Hi Mark,

Thank you very much for your reply and information. Through a combination of your post and playing around with PowerShell commands I have managed to get it to work.

It was an error in my PowerShell command.

Thank you very much again for your help.