I'm trying to create an Azure VM using PowerShell. I'm able to successfully create one, but the problem I have is that I'm trying to get the new VM to use an existing VNET and Subnet in a different resource group. When I run my code, it tries to create a brand new VNET and Subnet. I don't know how to link it to use the existing VNET which resides in a different resource group.
# Define Local Variables
$RG = "TestLab"
$Location = "eastus"
$UserName = "azadmin"
$Password = (ConvertTo-SecureString "Passw0rd123!" -Force -AsPlainText)
$VMName = "TestBox-VM-PS"
$VMSize = "Standard_D2as_V4"
$VirtualNetwork = "azTestBox01_vnet" #existing Vnet on different resource group
$Subnet = "TestBOX-Sub-10.0.0.0" #existing subnet on different resource group
$Friendly_Image_Name = "MicrosoftWindowsServer:WindowsServer:2019-Datacenter:Latest"
# Set credentials
$Credential = New-Object System.Management.Automation.PSCredential ($UserName, $Password)
New-AzVm `
-Name $VMName `
-ResourceGroupName $RG `
-Location $Location `
-VirtualNetworkName $VirtualNetwork `
-SubnetName $Subnet `
-Image $Friendly_Image_Name `
-Size $VMSize `
-Credential $Credential