-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateVM.ps1
123 lines (93 loc) · 2.9 KB
/
CreateVM.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#Connect Azure with Powershell
Install-Module -Name Az -AllowClobber -Scope CurrentUser
#Connect and Authenticate
Connect-AzAccount
#Get Current Connected Subscription/Context
Get-AzContext
#Get All Azure Subscriptions
Get-AzSubscription | more
#Select Azure Subscription
Select-AzContext
$rg = Get-AzResourceGroup `
-Name 'nc-demo-rg' `
-Location 'westeurope'
$rg
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name 'nc-demo-subnet-2' `
-AddressPrefix '10.34.2.0/24'
$subnetConfig
$vnet = New-AzVirtualNetwork `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-Name 'nc-demo-vnet-2' `
-AddressPrefix '10.34.0.0/16' `
-Subnet $subnetConfig
$vnet
$pip = New-AzPublicIpAddress `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-Name 'nc-demo-linux-2-pip-1' `
-AllocationMethod Static
$pip
$rule1 = New-AzNetworkSecurityRuleConfig `
-Name ssh-rule `
-Description 'Allow SSH' `
-Access Allow `
-Protocol Tcp `
-Direction Inbound `
-Priority 100 `
-SourceAddressPrefix Internet `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 22
$rule1
$nsg = New-AzNetworkSecurityGroup `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-Name 'nc-demo-linux-nsg-2' `
-SecurityRules $rule1
$nsg | more
$subnet = $vnet.Subnets | Where-Object { $_.Name -eq 'nc-demo-vnet-2' }
$nic = New-AzNetworkInterface `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-Name 'nc-demo-linux-2-nic-1' `
-Subnet $subnet `
-PublicIpAddress $pip `
-NetworkSecurityGroup $nsg
$nic
$LinuxVmConfig = New-AzureRmVMConfig `
-VMName 'nc-demo-ubuntu-2' `
-VMSize 'Standard_D1'
$password = ConvertTo-SecureString 'password12345&&%%++' -AsPlainText -Force
$linuxvmcred = New-Object System.Management.Automation.PSCredential ('ncvmadmin', $password)
$LinuxVmConfig = Set-AzVMOperatingSystem `
-VM $LinuxVmConfig `
-Linux `
-ComputerName 'nc-demo-ubuntu-2' `
-DisablePasswordAuthentication `
-Credential $linuxvmcred
$sshPublicKey = Get-Content "~/.ssh/id_rsa.pub"
Add-AzVMSshPublicKey `
-VM $LinuxVmConfig `
-KeyData $sshPublicKey `
-Path "/home/ncvmadmin/.ssh/authorized_keys"
Get-AzVMImageSku -Location $rg.Location -PublisherName "Canonical" -Offer "Ubuntu"
$LinuxVmConfig = Set-AzVMSourceImage `
-VM $LinuxVmConfig `
-PublisherName 'Canonical' `
-Offer 'UbuntuServer' `
-Skus '18.04-LTS' `
-Version 'latest'
$LinuxVmConfig = Add-AzRmVMNetworkInterface `
-VM $LinuxVmConfig `
-Id $nic.Id
New-AzVM `
-ResourceGroupName $rg.ResourceGroupName `
-Location $rg.Location `
-VM $LinuxVmConfig
$MyIP = Get-AzPublicIpAddress `
-ResourceGroupName $rg.ResourceGroupName `
-Name $pip.Name | Select-Object -ExpandProperty IpAddress
$MyIP
ssh -l ncvmadmin $MyIP