Home > Enterprise >  Create azure virtual network peering from powershell
Create azure virtual network peering from powershell

Time:10-13

While trying to write a script to help automate creating VPNs into our azure environment I got this weird error and I am not sure how to handle it I was hoping someone in the community could give some input.

I run a command like

Add-AzVirtualNetworkPeering -Name $peerNewVNet -VirtualNetwork $vNetObj -RemoteVirtualNetworkId $coreVNetID -UseRemoteGateways

but get an error because I'm passing the $coreVNetID as a string it seems.

The error says:

*

Add-AzVirtualNetworkPeering : Invalid format of the resource identifier.
Parameter name: idFromServer

I tried creating an object like this

(Get-AzVirtualNetwork -Name $coreVNet -ResourceGroupName $coreRG).Id

The issue is I'm in a different azure subscription than where this VNET is so I would have to change subscriptions to get the ID and then change back to create this peering I think? I did also try to pipe Select-AzSubscription but it doesn't like piping with azure commands much. Any help is appreciated.

CodePudding user response:

# Peer myVnetA to myVnetB.
$vNetA=Get-AzVirtualNetwork -Name myVnetA -ResourceGroupName myResourceGroupA
Add-AzVirtualNetworkPeering `
  -Name 'myVnetAToMyVnetB' `
  -VirtualNetwork $vNetA `
  -RemoteVirtualNetworkId "/subscriptions/<SubscriptionB-Id>/resourceGroups/myResourceGroupB/providers/Microsoft.Network/virtualNetworks/myVnetB"

There is an explaination of how to do this in powershell here: https://docs.microsoft.com/en-us/azure/virtual-network/create-peering-different-subscriptions#powershell

  • Related