-
Notifications
You must be signed in to change notification settings - Fork 7
/
15 az.compute - az-powershell-basics.ps1
75 lines (61 loc) · 2.09 KB
/
15 az.compute - az-powershell-basics.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
# Az.Compute
#
Exit #Prevent F5
# Put your settings into this _my-settings.ps1 file to use these scripts
. ./_my-settings.ps1
# See commands, their nouns and verbs to get a high level sense of what we can do with this module
Get-Command -Module Az.Compute
# See verbs
Get-Command -Module Az.Compute | Group-Object -Property Verb
# See nouns
Get-Command -Module Az.Compute | Group-Object -Property Noun
# List VMs
Get-AZVM
Get-AZVM | Select-Object *
# Create a VM
# Refer https://docs.microsoft.com/en-us/powershell/azure/azureps-vm-tutorial
# Make a credential with a user and password you'll remember
Get-Help Get-Credential -Examples
#$cred = Get-Credential -User dcadmin
# OR use KeyVault secret to build a credential
#Retrieve sysadmin password from KeyVault, keep as a secure string
$AdminPasswordSecret = Get-AzKeyVaultSecret `
-vaultName $myVaultName `
-name "AdminPassword"
# Build creds for local sysadmin
$UserName = 'dcadmin'
$cred = New-Object System.Management.Automation.PSCredential ($UserName, $AdminPasswordSecret.SecretValue)
$AdminPasswordSecret | Get-Member
$AdminPasswordSecret.SecretValueText
$cred
$myResourceGroupName
# use splatting to set all params at once
# see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting
$vmParams = @{
ResourceGroupName = $myResourceGroupName
Name = "$myResourceGroupName-VM1"
Location = $myRG.Location
ImageName = "Win2016Datacenter"
PublicIpAddressName = "$myResourceGroupName-PublicIp1"
Credential = $cred
OpenPorts = 3389
}
# Create the VM
$newVM1 = New-AzVM @vmParams
$newVM1
#Connect to that VM!
mstsc /v $newVM1.FullyQualifiedDomainName
# testing
# mstsc /v passtest-rg-vm1-63211f.eastus2.cloudapp.azure.com
#Stop to save $$$
$newVM2 | Stop-AzVM -Force
# or
Stop-AzVM -Name dpsugVM1 -ResourceGroupName $myResourceGroupName
# or stop all
Get-AzVM -ResourceGroupName $myResourceGroupName |
Stop-AzVM -Force
Get-AzVM -Status
#Start it later?
# or
Start-AzVM -Name dpsugVM1 -ResourceGroupName $myResourceGroupName
Get-AzVM -Status