Skip to content

Commit 5b78192

Browse files
authored
Add helpful New-AzVmConfig examples (#21923)
* examples * example fix * clean * changelog * clean * clean
1 parent b3aed16 commit 5b78192

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
-->
2222
## Upcoming Release
23+
* Added useful examples to the `New-AzVMConfig` help doc.
2324

2425
## Version 6.0.0
2526
* Added new switch parameter `OSImageScheduledEventEnabled` and string parameter `OSImageScheduledEventNotBeforeTimeoutInMinutes` to the cmdlets `New-AzVmssConfig` and `Update-AzVmss`.

src/Compute/Compute/help/New-AzVMConfig.md

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,107 @@ See [Quickstart: Create a Windows virtual machine in Azure with PowerShell](http
5050

5151
## EXAMPLES
5252

53-
### Example 1: Create a virtual machine object
53+
### Example 1: Create a virtual machine resource
5454
```powershell
55-
$AvailabilitySet = Get-AzAvailabilitySet -ResourceGroupName "ResourceGroup11" -Name "AvailabilitySet03"
56-
$VirtualMachine = New-AzVMConfig -VMName "VirtualMachine07" -VMSize "Standard_A1" -AvailabilitySetID $AvailabilitySet.Id -Zone "1"
55+
$rgname = "resourceGroupName";
56+
$loc = "eastus";
57+
58+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
59+
60+
# General Setup
61+
$vmname = 'v' + $rgname;
62+
$domainNameLabel = "d1" + $rgname;
63+
$vmSize = 'Standard_DS3_v2';
64+
$computerName = "c" + $rgname;
65+
66+
# Credential. Input Username and Password values
67+
$user = "";
68+
$securePassword = "" | ConvertTo-SecureString -AsPlainText -Force;
69+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
70+
71+
# Creating a VMConfig
72+
$vmconfig = New-AzVMConfig -VMName $vmname -vmsize $vmsize;
73+
74+
# Set source image values
75+
$publisherName = "MicrosoftWindowsServer";
76+
$offer = "WindowsServer";
77+
$sku = "2019-DataCenter";
78+
$vmconfig = Set-AzVMSourceImage -VM $vmconfig -PublisherName $publisherName -Offer $offer -Skus $sku -Version 'latest';
79+
80+
# NRP Setup
81+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
82+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
83+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
84+
$subnetId = $vnet.Subnets[0].Id;
85+
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Static -DomainNameLabel $domainNameLabel;
86+
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
87+
$pubipId = $pubip.Id;
88+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
89+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
90+
$nicId = $nic.Id;
91+
92+
$vmconfig = Add-AzVMNetworkInterface -VM $vmconfig -Id $nicId;
93+
$vmconfig = Set-AzVMOperatingSystem -VM $vmconfig -Windows -ComputerName $computerName -Credential $cred;
94+
95+
# Create the VM
96+
New-AzVM -ResourceGroupName $rgname -Location $loc -Vm $vmconfig;
97+
$vm = Get-AzVM -ResourceGroupName $rgname -Name $vmname;
98+
5799
```
58100

59-
The first command gets the availability set named AvailabilitySet03 in the resource group named ResourceGroup11, and then stores that object in the $AvailabilitySet variable.
60-
The second command creates a virtual machine object, and then stores it in the $VirtualMachine variable.
61-
The command assigns a name and size to the virtual machine.
62-
The virtual machine belongs to the availability set stored in $AvailabilitySet.
101+
### Example 2: Create a virtual machine object in a virtual machine scale set with fault domains setup
102+
```powershell
103+
$rgname = "resourceGroupName";
104+
$loc = "eastus";
105+
$vmname = "vm" + $rgname;
106+
107+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
108+
109+
$domainNameLabel = "d1" + $rgname;
110+
$vmname = "v" + $rgname;
111+
$vnetname = "myVnet";
112+
$vnetAddress = "10.0.0.0/16";
113+
$subnetname = "slb" + $rgname;
114+
$subnetAddress = "10.0.2.0/24";
115+
$vmssName = "vmss" + $rgname;
116+
$faultDomainNumber = 2;
117+
$vmssFaultDomain = 3;
118+
119+
$OSDiskName = $vmname + "-osdisk";
120+
$NICName = $vmname+ "-nic";
121+
$NSGName = $vmname + "-NSG";
122+
$OSDiskSizeinGB = 128;
123+
$VMSize = "Standard_DS2_v2";
124+
$PublisherName = "MicrosoftWindowsServer";
125+
$Offer = "WindowsServer";
126+
$SKU = "2019-Datacenter";
127+
128+
# Credential. Input Username and Password values.
129+
$user = "";
130+
$securePassword = "" | ConvertTo-SecureString -AsPlainText -Force;
131+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
132+
133+
$frontendSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetname -AddressPrefix $subnetAddress;
134+
$vnet = New-AzVirtualNetwork -Name $vnetname -ResourceGroupName $rgname -Location $loc -AddressPrefix $vnetAddress -Subnet $frontendSubnet;
135+
136+
$vmssConfig = New-AzVmssConfig -Location $loc -PlatformFaultDomainCount $vmssFaultDomain;
137+
$vmss = New-AzVmss -ResourceGroupName $RGName -Name $VMSSName -VirtualMachineScaleSet $vmssConfig;
138+
139+
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name RDP -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow;
140+
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $RGName -Location $loc -Name $NSGName -SecurityRules $nsgRuleRDP;
141+
$nic = New-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName -Location $loc -SubnetId $vnet.Subnets[0].Id -NetworkSecurityGroupId $nsg.Id -EnableAcceleratedNetworking;
142+
143+
# VM
144+
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $VMSize -VmssId $vmss.Id -PlatformFaultDomain $faultDomainNumber ;
145+
Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential $cred ;
146+
Set-AzVMOSDisk -VM $vmConfig -StorageAccountType "Premium_LRS" -Caching ReadWrite -Name $OSDiskName -DiskSizeInGB $OSDiskSizeinGB -CreateOption FromImage ;
147+
Set-AzVMSourceImage -VM $vmConfig -PublisherName $PublisherName -Offer $Offer -Skus $SKU -Version latest ;
148+
Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id;
149+
150+
New-AzVM -ResourceGroupName $RGName -Location $loc -VM $vmConfig;
151+
$vm = Get-AzVM -ResourceGroupName $rgname -Name $vmName;
152+
153+
```
63154

64155
## PARAMETERS
65156

0 commit comments

Comments
 (0)