Skip to content

Instantly share code, notes, and snippets.

@weeyin83
Created January 6, 2020 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weeyin83/81e7a7bf3caf3d0bce787db5d562b47e to your computer and use it in GitHub Desktop.
Save weeyin83/81e7a7bf3caf3d0bce787db5d562b47e to your computer and use it in GitHub Desktop.
This script will deploy a virtual machine within Azure utilising GitHub Actions as the deployment technology.
<#
.SYNOPSIS
Deploys a virtual machine in Azure using GitHub Actions
.DESCRIPTION
This script will deploy a virtual machine within Azure utilising GitHub Actions as the deployment technology.
.OUTPUTS
.NOTES
Written by: Sarah Lean
Find me on:
* My Blog: http://www.techielass.com
* Twitter: https://twitter.com/techielass
* LinkedIn: http://uk.linkedin.com/in/sazlean
* GitHub: https://www.github.com/weeyin83
.EXAMPLE
Change Log
V1.00, 06/01/2020 - Initial version
License:
The MIT License (MIT)
Copyright (c) 2017 Sarah Lean
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[string]
$servicePrincipal,
[Parameter(Mandatory = $True)]
[string]
$servicePrincipalSecret,
[Parameter(Mandatory = $True)]
[string]
$servicePrincipalTenantId,
[Parameter(Mandatory = $True)]
[string]
$azureSubscriptionName,
[Parameter(Mandatory = $True)]
[string]
$resourceGroupName,
[Parameter(Mandatory = $True)]
[string]
$resourceGroupNameRegion,
[Parameter(Mandatory = $True)]
[string]
$serverName,
[Parameter(Mandatory = $True)]
[string]
$adminLogin,
[Parameter(Mandatory = $True)]
[String]
$adminPassword
)
#region Login
# This logs into Azure with a Service Principal Account
#
Write-Output "Logging in to Azure with a service principal..."
az login `
--service-principal `
--username $servicePrincipal `
--password $servicePrincipalSecret `
--tenant $servicePrincipalTenantId
Write-Output "Done"
Write-Output ""
#endregion
#region Subscription
#This sets the subscription the resources will be created in
Write-Output "Setting default azure subscription..."
az account set `
--subscription $azureSubscriptionName
Write-Output "Done"
Write-Output ""
#endregion
#region Create Resource Group
# This creates the resource group used to house the VM
Write-Output "Creating resource group $resourceGroupName in region $resourceGroupNameRegion..."
az group create `
--name $resourceGroupName `
--location $resourceGroupNameRegion
Write-Output "Done creating resource group"
Write-Output ""
#endregion
#region Create VM
# Create a VM in the resource group
Write-Output "Creating VM..."
try {
az vm create `
--resource-group $resourceGroupName `
--name $serverName `
--image win2016datacenter `
--admin-username $adminLogin `
--admin-password $adminPassword
}
catch {
Write-Output "VM already exists"
}
Write-Output "Done creating VM"
Write-Output ""
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment