1

I am trying to powershell script (Az latest) an existing vnet peering configuration using add-azvirtualnetworkpeering. The two vnets are in different subscriptions. It appears I must use a variable for the virtualnetwork and remotenetwork switches. I can set my subscription context for 1 and apply get-azvirtualnetwork assigning it to $vnet1, then switch context to the other subscription context and assign the second $vnet2 value. But when I run the add-azvirtualnetworkpeering command (from either subscription context) it gives me A parameter cannot be found that matches parameter 'virtualnetwork' or 'remotenetwork' depending on which context I am in. See example 2 here https://docs.microsoft.com/en-us/powershell/module/az.network/add-azvirtualnetworkpeering?view=azps-1.6.0 except the RG is already created and I use a get-azvirtualnetwork in place of the new-azvirtualnetwork since they already exist. Code looks like this:

PS C:\>set-azcontext (get-azsubscription -subscriptionname "1")
PS C:\>$vnet1 = get-azvirtualnetwork -name "vnet1"
PS C:\>set-azcontext (get-azsubscription -subscriptionname "2")
PS C:\>$vnet2 = get-azvirtualnetwork -name "vnet2"
PS C:\>add-azvirtualnetworkpeering -name "vnet1tovnet2" -virtualnetwork $vnet1 -remotenetwork $vnet2.id -AllowForwardedTraffic

I get this error:

Add-AzVirtualNetworkPeering : A parameter cannot be found that matches parameter name 'remotenetwork'.
At line:1 char:99
+ ... vnet1tovnet2" -virtualnetwork $vnetfrom -remotenetwork $vnet2 ...
+                                                    ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-AzVirtualNetworkPeering], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.Network.AddAzureVirtualNetworkPeeringCommand
1

If your vNets are in the same tenant then using set-azcontext is overkill, just use select-azsubscription to change the subscription you are working with, and this won't touch any of your variables.

However, your parameters are also wrong. Check the documentation for add-azvirtualnetworkpeering the parameters are VirtualNetwork and RemoteVirtualNetworkId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.