简体   繁体   中英

How do I create Subnet in existing Vnet

I am using Classic Portal and I have Vnet "RGVnet" configured with 10.0.0.0/16 Subnet :- 10.0.0.0/24 Configured.

I want to add a new subnet in the same Vnet without using Get-AzureVNetConfig -ExportToFile c:\\NetworkConfig.xml .

Is there any other way to add subnet in classic portal like we add in RM Vnet?

Appreciate your assistance. :)

You do not need to ExportToFile to create a subnet in a vnet:

New-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name RGVnet`
    -AddressPrefix 10.0.0.0/16 -Location centralus   
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName TestRG -Name TestVNet
Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd `
    -VirtualNetwork $vnet -AddressPrefix 10.0.0.0/24
Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd `
    -VirtualNetwork $vnet -AddressPrefix 10.0.1.0/24
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet 

The last step applies the configuration.

MSDN virtual-networks-create-vnet-arm-ps

No.

There is no such a simple PowerShell command that will add a subnet to a classic Vnet. If you check the ARM REST API for both Microsoft/ClassicNetwork and Microsoft/Network, you will see that in Resource Manager model, "subnets" is managed both as resources under a Vnet and as a property in Vnet, while in classic model, "subnets" is just a property in Vnet.

Since "subnets" is just a property in Vnet for classic model, and "subnets" is an array of objects, you always need at least 2 requests to update "subnets". One is to get the old subnets, and the other is to put the subnets after appending the new subnets to the old subnets.

However, if you use REST API instead of pure Azure PowerShell, you don't need to export the xml file at all. Here is a script I wrote.

# Adding the AD library to your PowerShell Session.
Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'

# This is the tenant name of you subscription. You can use tenant id instead if you want.
$tenantName = "<you tenant name>"
$authString = "https://login.windows.net/" + $tenantName

# Create a authentication context with the above authentication string.
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ($authString, $false)

# Client Id and key of your AD application. You can create an AD application through Azure
# Portal or PowerShell.
$clientId = "<the client id of your AD application>"
$key = "<the key of your AD application>"

# Create a client credential with the above client id and key.
$clientCred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential ($clientId, $key)

# The resource URI for your token. If you are using ARM, "https://management.azure.com/"
# is good too.
$resource = "https://management.core.windows.net/"

# Acquire access token from server.
$authenticationResult = $authenticationContext.AcquireToken($resource, $clientCred);

# Use the access token to setup headers for your http request.
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"}

# Send a request to get the Vnet you want to update.
$vnet = Invoke-RestMethod -Method GET -Uri "https://management.azure.com/subscriptions/<your subscription id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/<your vnet>?api-version=2016-04-01" -Headers $headers

# Create a new subnet from a Json string. and add it to the subnets of your Vnet.
$newSubnet = ConvertFrom-Json '{"name": "newSubnet","addressPrefix": "10.34.0.0/29"}'
$vnet.properties.subnets += $newSubnet

# Convert the Vnet object into a Json string. This will be used as request body in the second http request.
$body = ConvertTo-Json $vnet -Depth 3

# Send another http request to update your Vnet.
Invoke-RestMethod -Method PUT -Uri "https://management.azure.com/subscriptions/<your subscription id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/<your vnet>?api-version=2016-04-01" -Headers $headers -Body $body

Note: in order to use my script, you need to follow this tutorial to create a service principal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM