如何在现有Vnet中创建子网

时间:2016-04-25 06:04:59

标签: powershell azure azure-powershell azure-virtual-network

我正在使用Classic Portal,我的Vnet“RGVnet”配置为10.0.0.0/16 子网: - 10.0.0.0/24已配置。

我想在不使用Get-AzureVNetConfig -ExportToFile c:\NetworkConfig.xml的情况下在同一个Vnet中添加新子网。

有没有其他方法可以在经典门户中添加子网,就像我们在RM Vnet中添加一样?

感谢您的帮助。 :)

2 个答案:

答案 0 :(得分:1)

您无需ExportToFile即可在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 

最后一步应用配置。

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

答案 1 :(得分:1)

没有

没有这样一个简单的PowerShell命令可以为经典的Vnet添加子网。如果检查Microsoft / ClassicNetwork和Microsoft / Network的ARM REST API,您将看到在资源管理器模型中,“子网”既作为Vnet下的资源又作为Vnet中的属性进行管理,而在经典模型中,子网“只是Vnet中的一个属性。

由于“subnets”只是经典模型的Vnet中的属性,而“subnets”是一个对象数组,因此总是需要至少2个请求才能更新“子网”。一种是获取旧子网,另一种是在将新子网附加到旧子网之后放置子网。

但是,如果使用REST API而不是纯Azure PowerShell,则根本不需要导出xml文件。这是我写的剧本。

# 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

注意:要使用我的脚本,您需要按照this tutorial创建服务主体。

相关问题