-
Notifications
You must be signed in to change notification settings - Fork 2
/
New-VM.ps1
65 lines (53 loc) · 1.98 KB
/
New-VM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
param
(
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$VMName,
[Parameter(Mandatory = $true)]
[string]$VNetName,
[Parameter(Mandatory = $true)]
[string]$VNetResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$SubnetName,
[Parameter(Mandatory = $true)]
[string]$Location
)
# Set the output level to verbose and make the script stop on error
$VerbosePreference = "Continue"
$ErrorActionPreference = "Stop"
#$securePassword = ConvertTo-SecureString "Password" -AsPlainText -Force
#$cred = New-Object System.Management.Automation.PSCredential ("username", $securePassword);
$cred = Get-Credential
$rg = Get-AzureRmResourceGroup -Name $ResourceGroupName
$subnetConfig = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $VNetResourceGroupName | Get-AzureRmVirtualNetworkSubnetConfig `
-Name $SubnetName
$pipName = $VMName + 'IP'
$pip = New-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $rg.ResourceGroupName -Location $Location `
-AllocationMethod Static
$nicName = $VMName + 'NIC'
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rg.ResourceGroupName -Location $Location `
-SubnetId $subnetConfig.Id -PublicIpAddressId $pip.Id
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize Standard_A1_v2
$vm = Set-AzureRmVMOperatingSystem `
-VM $vm `
-Windows `
-ComputerName $VMName `
-Credential $cred `
-ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage `
-VM $vm `
-PublisherName MicrosoftWindowsServer `
-Offer WindowsServer `
-Skus 2016-Datacenter `
-Version latest
$osDiskName = $VMName + 'OsDisk'
$vm = Set-AzureRmVMOSDisk `
-VM $vm `
-Name $osDiskName `
-DiskSizeInGB 128 `
-CreateOption FromImage `
-Caching ReadWrite `
-StorageAccountType StandardLRS
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
New-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Location $Location -VM $vm