Status of newly created subnet does not change in powershell

Martin Thong 60 Reputation points
2024-04-17T05:47:17.47+00:00

I have the following code to create a subnet in an existing address space, 10.1.2.0/24. The subnet is created in the console after a minute but the response is stuck in the while loop. How can I resolve this?

$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName

$subnet = @{
    Name = 'newsubnet1'
    VirtualNetwork = $vnet
    AddressPrefix = '10.1.2.32/28'
}

$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnet
$vnet | Set-AzVirtualNetwork | Out-Null

while ($null -eq $subnet.Id) {
    Write-Log "Waiting for subnet1 to be fully provisioned..."
    Start-Sleep -Seconds 10
    $subnet = Get-AzVirtualNetworkSubnetConfig -Name "newsubnet1" -VirtualNetwork $vnet
}
Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,145 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. ChaitanyaNaykodi-MSFT 22,941 Reputation points Microsoft Employee
    2024-04-17T22:49:21.2333333+00:00

    @Martin Thong

    Thank you for reaching out.

    I tried the script on my end and ran into same issue, I think it will help if you could report this issue on the Azure PowerShell Repository here

    Meanwhile you can follow the script below as a workaround, where I have invoked Subnets - Get REST API to fetch the subnet provisioning state after the subnet is deployed.

    Connect-AzAccount
    $SubId = "SubID"
    Set-AzContext -Subscription $SubId
    
    $vnetName="vnetName"
    $resourceGroupName="resourceGroupName"
    $subnetName = "subnetName"
    $url = "https://management.azure.com/subscriptions/"+$SubId+"/resourceGroups/"+$resourceGroupName+"/providers/Microsoft.Network/virtualNetworks/"+$vnetName+"/subnets/"+$subnetName+"?api-version=2022-11-01"
    
    $token = (Get-AzAccessToken).token
    
    $headers = @{'Authorization' = "Bearer $token"}
    
    $vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
    
    $subnet = @{
        Name = $subnetName
        VirtualNetwork = $vnet
        AddressPrefix = '10.0.47.32/28'
    }
    
    $subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnet
    $vnet | Set-AzVirtualNetwork | Out-Null
    
    $result = Invoke-WebRequest -Method GET -Uri $url -Headers $headers -UseBasicParsing
    
    $body = $result.Content | ConvertFrom-Json
    
    $ProvState = $body.properties.provisioningState
    If ($ProvState -eq "Succeeded")
    {
    Write-Output "Provisioning successfull"
    }
    
    else
    {
    
    While ($ProvState -ne "Succeeded")
    {
    Start-Sleep -Seconds 10
    $result = Invoke-WebRequest -Method GET -Uri $url -Headers $headers -UseBasicParsing
    
    $body = $result.Content | ConvertFrom-Json
    
    $ProvState = $body.properties.provisioningState
    }
    }
    
    

    Hope this helps! Please let me know if you have any questions. Thank you!


    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments