Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jwstl
Created February 21, 2017 18:40
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 jwstl/9d081cd7ea276cc14700dc3069a9e80c to your computer and use it in GitHub Desktop.
Save jwstl/9d081cd7ea276cc14700dc3069a9e80c to your computer and use it in GitHub Desktop.
#
# File: Deploy-supersecret-Dan.ps1
# .\Templates\azuredeploysupersecretZZZ.parameters.json
# .\Templates\azuredeploysupersecretZZZ.json
#
#
# 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.
#
# Requires -Version 3.0
# Requires -Module AzureRM.Resources
# Requires -Module Azure.Storage
<#
.SYNOPSIS
Script to create a supersecret Azure virtual machine utilizing the Azure image "".
.DESCRIPTION
This script copies the vhd from a storage account into the vhd container.
It then creates all necessary supporting VM components in it's own resource group.
***********************************
***********************************
Edit before use:
File: Deploy-supersecret-Core611Clone.ps1
[string]$vmName = '<VM_NAME_HERE>'
File: azuredeploysupersecret-Core611Clone.json
"defaultValue": "<VM_NAME_HERE>"
File: azuredeploysupersecret-Core611Clone.parameters.json
"value": "<VM_NAME_HERE>"
***********************************
***********************************
.PARAMETER VERBOSE
The only paramater accepted by this script is -verbose
.EXAMPLE
./Deploy-supersecret-Dan.ps1
Creates VM with no feedback (allows for utilization of this script by another script)
.EXAMPLE
./Deploy-supersecret-Dan.ps1 -verbose
Creates VM with feedback in the console
.LINK
none
.NOTES
Editing the parameter "vmName" will cause this script to create a new virtual machine
with that name.
#>
<#
#######
This needs to be VERY Verbose
#######>
Param(
[string]$vmName = 'Core613a',
[string]$TemplateFile = 'C:\Code\Azure\Core613a\Templates\azuredeploy-Core613a.json',
[string]$TemplateParametersFile = 'C:\Code\Azure\Core613a\Templates\azuredeploy-Core613a.parameters.json'
)
## DO NOT EDIT BELOW THIS LINE ##
# Global Variables
$containerName = 'vhds'
$destBlobName = $vmName + '.vhd'
$srcUri = 'https://supersecretdemo.blob.core.windows.net/vhds/k2core613a-k2v4.7-fixed.vhd'
$srcStorageAccount = ([System.Uri]$srcUri).Host -replace '\.blob\.core\.windows\.net'
$srcResourceGroupName = 'supersecret-Demo'
$ResourceGroupName = $vmName + "-RGP"
$StorageAccountCASE = $vmName.ToLower()
$StorageAccountCHARS = "-_"
$StorageAccountCASE1 = $StorageAccountCASE -replace '[><_-]'
$StorageAccount = $StorageAccountCASE1 + 'str'
$azureSubscription = 'supersecret'
$OptionalParameters = New-Object -TypeName Hashtable
$ResourceGroupLocation = 'East US 2'
$StorageAccountName = $StorageAccount
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile)
$TemplateParametersFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile)
Import-Module Azure -ErrorAction SilentlyContinue
$cred = Get-Credential
Add-AzureRmAccount -Credential $cred
Select-AzureRmSubscription -SubscriptionName $azureSubscription
try {
[Microsoft.Azure.Common.Authentication.AzureSession]::ClientFactory.AddUserAgent("VSAzureTools-$UI$($host.name)".replace(" ","_"), "2.8")
} catch { }
Set-StrictMode -Version 3
function copyVHD {
# Get SOURCE storage account key
$Keys = Get-AzureRmStorageAccountKey -ResourceGroupName $srcResourceGroupName -Name $srcStorageAccount
$srcStorageKey = $Keys[0].Value
# Destination Storage Account
$destStorageAccount = $StorageAccountName
# Get DESTINATION storage account key
$Keys = Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$destStorageKey = $Keys[0].Value
# Create the source storage account context
$srcContext = New-AzureStorageContext -StorageAccountName $srcStorageAccount -StorageAccountKey $srcStorageKey
# Create the destination storage account context
$destContext = New-AzureStorageContext -StorageAccountName $destStorageAccount -StorageAccountKey $destStorageKey
# Create storage container
New-AzureStorageContainer -Name $containerName -Context $destContext
# Start the asynchronous copy - specify the source authentication with -SrcContext
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri -SrcContext $srcContext -DestContainer $containerName -DestBlob $destBlobName -DestContext $destContext
# Retrieve the current status of the copy operation
$status = $blob1 | Get-AzureStorageBlobCopyState
# Print out status
$status
# Loop until complete
While($status.Status -eq "Pending"){
$status = $blob1 | Get-AzureStorageBlobCopyState
Start-Sleep 60
# Print out status
$status
Write-Host "Next status check in 60 seconds ..." -ForegroundColor Yellow
}
}
# Create or update the resource group using the specified template file and template parameters file
function createResourceGroup {
$resourceGroupExists = Get-AzureRmResourceGroup -Location $ResourceGroupLocation -Name $ResourceGroupName -ErrorAction SilentlyContinue
if ($resourceGroupExists -eq $null) {
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -Force -ErrorAction Stop | Out-Null # wait for process to complete with "out-null"
}
}
function createStorage {
If($StorageAccountName)
{
Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction SilentlyContinue `
-ErrorVariable IsExistStorageError | Out-Null
#Check if storage account is exist
If($IsExistStorageError.Exception -eq $null)
{
Write-Host "Storage account $StorageAccountName already exists." -ForegroundColor Yellow
exit
} else {
Write-Host "Creating storage account $StorageAccountName ..." -ForegroundColor Green
New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName `
-Name $StorageAccountName `
-Location $ResourceGroupLocation `
-Type "Standard_LRS" `
-ErrorVariable createStorageError | Out-Null
}
}
}
# Deploy the azure template
function deployTemplate {
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
-Force -Verbose | Out-Null
}
# Display virtual machine and connection information
function displayInfo {
$pubEXT = "-PIP"
$pubIPName = "$($vmName)$($pubEXT)"
$vmPubIP = Get-AzureRmPublicIpAddress -ResourceGroupName $ResourceGroupName -Name $pubIPName
Write-Host " "
Write-Host " "
Write-Host "Your FQDN is: $($vmPubIP.DnsSettings.Fqdn)" -foregroundcolor red -backgroundcolor yellow
Write-Host "Your IP address is: $($vmPubIP.IpAddress)" -foregroundcolor red -backgroundcolor yellow
Write-Host " "
Write-Host " "
}
# Script actions
createResourceGroup
sleep -Seconds 120
createStorage
sleep -Seconds 180
#copyVHD
deployTemplate
displayInfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment