Forum Discussion
New-AzVM On An Existing Virtual Network - PowerShell
Hi,
I'm trying to create a VM with PowerShell to go on to the following, existing vNet/Subnet.
PS C:\ps> $VirtualNetwork = (Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $NetResourceGroupName)
PS C:\ps> $VirtualNetwork
Name : UKS-vNet1
ResourceGroupName : UKS-RG-Network-VNET1
Location : uksouth
Id : /subscriptions/3811c90f-9619-48fe-8f8f-abe981933b31/resourceGroups/UKS-RG-Network-VNET1/providers/
Microsoft.Network/virtualNetworks/UKS-vNet1
Etag : W/"3c25dfd5-7298-46df-9857-a61cd5fc237b"
ResourceGuid : a0692e55-37b8-40db-806f-ff402f5962a4
ProvisioningState : Succeeded
Tags :
AddressSpace : {
"AddressPrefixes": [
"10.3.0.0/16"
]
}
DhcpOptions : {
"DnsServers": []
}
Subnets : [
{
"Delegations": [],
"Name": "UKS-vNet1-sNet1-VMsOnvNet1",
"Etag": "W/\"3c25dfd5-7298-46df-9857-a61cd5fc237b\"",
"Id": "/subscriptions/3811c90f-9619-48fe-8f8f-abe981933b31/resourceGroups/UKS-RG-Network-VNET1
/providers/Microsoft.Network/virtualNetworks/UKS-vNet1/subnets/UKS-vNet1-sNet1-VMsOnvNet1",
"AddressPrefix": [
"10.3.1.0/24"
],
"IpConfigurations": [
{
"Id": "/subscriptions/3811c90f-9619-48fe-8f8f-abe981933b31/resourceGroups/UKS-RG-VM-TempTe
tVMs/providers/Microsoft.Network/networkInterfaces/uks-vm-testvm1749/ipConfigurations/ipconfig1"
}
],
"ServiceAssociationLinks": [],
"ResourceNavigationLinks": [],
"ServiceEndpoints": [],
"ServiceEndpointPolicies": [],
"PrivateEndpoints": [],
"ProvisioningState": "Succeeded",
"PrivateEndpointNetworkPolicies": "Enabled",
"PrivateLinkServiceNetworkPolicies": "Enabled",
"IpAllocations": []
}
]
VirtualNetworkPeerings : []
EnableDdosProtection : false
DdosProtectionPlan : null
PS C:\ps>
Creating the VM as follows:
$VMResourceGroupName = "UKS-RG-VM-TempTetVMs"
$VMName = "UKS-VM-TestVM2"
#Create VM
New-AzVm `
-ResourceGroupName $VMResourceGroupName `
-Location $Location `
-VirtualNetworkName "UKS-vNet1" `
-SubnetName "UKS-vNet1-sNet1-VMsOnvNet1" `
-credential $credential `
-size Standard_B1ms `
-Name $VMName
However, it does not put the VM on to the existing UKS-vNet1 Virtual Network, instead creates a new one (in the VM Resource Group) and puts it on a new subnet that it also creates.
I've tried various options when scripting... entering the -VirtualNetwork in plain text (as shown here) using variables (as in $VirtualNetwork and $Subnet) including with various Properties (.Name .ID etc)
The Help for the parameter suggests it's just a system.string value
PS C:\ps> help new-azvm -Parameter SubnetName
-SubnetName <System.String>
The name of a new (or existing) subnet for the created VM to use. If not specified, a name will be generated.
PS C:\ps> help new-azvm -Parameter VirtualNetworkName
-VirtualNetworkName <System.String>
The name of a new (or existing) virtual network for the created VM to use. If not specified, a name will be generated.
This method works...
https://docs.microsoft.com/en-us/azure/virtual-network/quick-create-powershell
The only thing different I'm doing is having a different Resource Group for vNets and VMs, which does work when creating the VM in the GUI.
I'm about stuck now, so any help would be very much appreciated,
Thanks in advance.
JH
- Jayh-Copper Contributor
Additional Info:
I've just re-tried the method shown in the Quickstart, directly copying the scripts from the page (with the copy button) and that produces the same problem.
TIA,
JH
- hspintoMicrosoft
to better define the desired outcome, you should explicitly specify each resource and parameter that will make up your VM. If you create the NIC first and then add the NIC to the VM spec, you'll have total control over where those resources are created. Something like this:
$VMResourceGroupName = "UKS-RG-VM-TempTetVMs" $VMName = "UKS-VM-TestVM2" $VirtualNetwork = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $NetResourceGroupName $NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $VMResourceGroupName -Location $Location -SubnetId $VirtualNetwork.Subnets[0].Id $VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize Standard_B1ms $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VMName -Credential $credential -ProvisionVMAgent -EnableAutoUpdate $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version latest New-AzVM -ResourceGroupName $VMResourceGroupName -Location $Location -VM $VirtualMachine -Verbose