Forum Discussion
Jayh-
Jun 18, 2020Copper Contributor
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 $N...
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
hspinto
Jun 21, 2020Microsoft
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