Warm tip: This article is reproduced from stackoverflow.com, please click
azure-cli powershell

How to retrieve the proper Subnet Resource Id to create a virtual network rule?

发布于 2020-04-23 17:07:10

I created a virtual network like so:

$virtualNetwork = New-AzureRmVirtualNetwork -Name MyVirtualNetwork -ResourceGroupName TestResourceGroup -Location centralus -AddressPrefix "10.0.0.0/16"

Then I created a subnet for it and then set the virtual network:

Add-AzureRmVirtualNetworkSubnetConfig -Name backendSubnet -VirtualNetwork $virtualNetwork -AddressPrefix "10.0.2.0/24"
$virtualNetwork | Set-AzureRmVirtualNetwork

I then get my newly-created subnet and give it an Id:

$subnet = $virtualNetwork | Get-AzureRmVirtualNetworkSubnetConfig -Name backendSubnet
$subnet.Id = "resourceId"

And try to add this to my AzureRmDataLakeStore, but it fails:

Add-AzureRmDataLakeStoreVirtualNetworkRule -Account MyAccount -Name VNRule -SubnetId $subnet.Id
Add-AzureRmDataLakeStoreVirtualNetworkRule: Subnet resource ID 'resourceId' is invalid.

Does anyone know what I should be entering for the -SubnetId flag? Thank you.

Solution from start to finish (thank you @AdminOfThings):

$virtualNetwork = New-AzureRmVirtualNetwork -Name MyVirtualNetwork -ResourceGroupName TestResourceGroup -Location centralus -AddressPrefix "10.0.0.0/16"
Add-AzureRmVirtualNetworkSubnetConfig -Name backendSubnet -VirtualNetwork $virtualNetwork -AddressPrefix "10.0.2.0/24"
$virtualNetwork = $virtualNetwork | Set-AzureRmVirtualNetwork
Add-AzureRmDataLakeStoreVirtualNetworkRule -Account MyAccount -Name VNRule -SubnetId $subnet.Id
Questioner
C. Lightfoot
Viewed
29
AdminOfThings 2020-02-12 07:30

In your case, $virtualNetwork and $subnet will contain the subnetID short name. The subnetID syntax you are attempting to enter is the Name property of the PSSubnet object.

Add-AzureRmDataLakeStoreVirtualNetworkRule -Account MyAccount -Name VNRule -SubnetId $subnet.Name

It appears you want to give $subnet a new ID and then pass that into Add-AzureRmDataLakeStoreVirtualNetworkRule. When you update $subnet.Id, you are only updating a variable and not the backend Azure object. I don't know that it is even possible to update that object's property and have it reflect in Azure without recreating it.

When you update the properties in $virtualNetwork and execute Set-AzureRmVirtualNetwork, you will need to store that command output into $virtualNetwork again. Only then will $virtualNetwork contain the updated object as it exists in Azure.