Skip to content

Instantly share code, notes, and snippets.

@duartefdias
Created December 30, 2020 15:04
Show Gist options
  • Save duartefdias/9b1c917b9cc101b6dff6542868687876 to your computer and use it in GitHub Desktop.
Save duartefdias/9b1c917b9cc101b6dff6542868687876 to your computer and use it in GitHub Desktop.
Create VM in Azure powershell script
# Login to Azure using Service Principal credentials from Github Secrets
Write-Output "Logging in to Azure with a service principal..."
az login `
--service-principal `
--username $Env:SP_CLIENT_ID `
--password $Env:SP_CLIENT_SECRET `
--tenant $Env:SP_TENANT_ID
Write-Output "Done"
# Select Azure subscription
az account set `
--subscription $Env:AZURE_SUBSCRIPTION_NAME
# Create the VM configuration object
$ResourceGroupName = "my-resource-group"
$VmName = "Demo-vm-from-gh"
# Create a VM in Azure
Write-Output "Creating VM..."
try {
az vm create `
--resource-group $ResourceGroupName `
--name $VmName `
--image win2016datacenter `
--admin-password $Env:SP_CLIENT_SECRET `
}
catch {
Write-Output "VM already exists"
}
Write-Output "Done creating VM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment