Install-Module Az.Compute,Az.Resources,Az.KeyVault -Force
First connect to your Azure account.
Connect-AzAccount
If your account contains more than one active subscription the first one will be selected for further use. To select another subscription, use Set-AzContext.
$myAzSubscription = "My Subscription"
Set-AzContext -Subscription $myAzSubscription
These variables are used throughout the notebook so set them at the top so they can be used everywhere.
NOTE: This also means that all you have to do is change the vaules here and the Notebook will just work still (so long as the values are correct)
# Used all over
$RESOURCE_GROUP_NAME = 'JupyterTest'
$LOCATION = 'East US 2'
# Resource names
$VAULT_NAME = 'myAzVault'
$VM_NAME = 'myAzVM'
# Single instances
$VM_USERNAME = 'azureuser'
$VM_IMAGE = 'UbuntuLTS'
New-AzResourceGroup
command¶An Azure resource group is a logical container into which Azure resources are deployed and managed. A resource group must be created before a virtual machine. In the following example, a resource group named myResourceGroupVM is created in the EastUS region:
New-AzResourceGroup -ResourceGroupName $RESOURCE_GROUP_NAME -Location $LOCATION
We will use this to store the password to our VM for future use.
New-AzKeyVault -Name $VAULT_NAME -Location $LOCATION -ResourceGroupName $RESOURCE_GROUP_NAME
$secret = [System.IO.Path]::GetRandomFileName() | ConvertTo-SecureString -AsPlainText
Set-AzKeyVaultSecret -VaultName $VAULT_NAME -SecretValue $secret -Name VMpassword
New-AzVM
command¶$cred = [pscredential]::new($VM_USERNAME, $secret)
$splat = @{
Image = $VM_IMAGE
Credential = $cred
ResourceGroupName = $RESOURCE_GROUP_NAME
Location = $LOCATION
Name = $VM_NAME
}
New-AzVM @splat
At this point you should be able to run Get-AzVM
and your VM should show up.
Get-AzVM -Name $VM_NAME -Status