【问题标题】:How do I create Subnet in existing Vnet如何在现有 Vnet 中创建子网
【发布时间】:2016-08-18 09:40:08
【问题描述】:

我正在使用经典门户,并且我已将 Vnet“RGVnet”配置为 10.0.0.0/16 子网:- 10.0.0.0/24 已配置。

我想在同一个 Vnet 中添加一个新子网,而不使用 Get-AzureVNetConfig -ExportToFile c:\NetworkConfig.xml

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

感谢您的帮助。 :)

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 嗨彼得,我猜上面的命令是用于资源管理器部署而不是经典。
    • 是的,你是对的,我想念你的问题。感谢您指出。
    • 没问题,彼得 :) 如果我们能在经典部署中获得它,我们将不胜感激。
    【解决方案2】:

    没有。

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

    由于“子网”只是 Vnet 中经典模型的一个属性,而“子网”是一个对象数组,因此您总是需要至少 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 创建服务主体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多