Last active
December 5, 2019 17:06
-
-
Save lrakai/2ec2530102c920177e8755eaf067aead to your computer and use it in GitHub Desktop.
AzureNestedHyperVPriv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Provide the subscription Id where managed disk is created | |
# subscriptionId=yourSubscriptionId # omit for use in Azure Cloud Shell | |
# az account set --subscription $subscriptionId | |
#Provide the name of your resource group where managed disk is created | |
resourceGroupName=QACA | |
#Provide the managed disk name | |
diskName=ca-lab-vm_OsDisk_1_b5b39d9313ec4ce3aa8ace3889dcce9b | |
#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600. | |
#Know more about SAS here: https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1 | |
sasExpiryDuration=3600 | |
#Provide storage account name where you want to copy the underlying VHD file of the managed disk. | |
storageAccountName=techvetsdisks | |
#Name of the storage container where the downloaded VHD will be stored | |
storageContainerName=hyperv-cpsa | |
#Provide the key of the storage account where you want to copy the VHD | |
storageAccountKey=<INSERT_STORAGE_ACCOUNT_KEY> | |
#Provide the name of the destination VHD file to which the VHD of the managed disk will be copied. | |
destinationVHDFileName=cpsa.vhd | |
sas=$(az disk grant-access --resource-group $resourceGroupName --name $diskName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv) | |
az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas | |
# Watch copy progress | |
# watch az storage blob show --container-name $storageContainerName --account-name $storageAccountName --name $destinationVHDFileName --query "properties.copy" | |
# Copy across subscriptions with azcopy (Disk can only be used in the same subscription) | |
# azcopy --source "https://acct1.blob.core.windows.net/hyperv-cpsa/cpsa.vhd" --destination "https://acct2.blob.core.windows.net/hyperv-cpsa/cpsa.vhd" --dest-key '...' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Azure Nested VV Host Configuration | |
.File Name | |
- HyperVHostConfig.ps1 | |
.What calls this script? | |
- This is a PowerShell DSC script called by azuredeploy.json | |
.What does this script do? | |
- Creates an Internal Switch in Hyper-V called "Nat Switch" | |
- Creates a NAT Network on 192.168.0.0/24. (All of your Nested VMs need static IPs on this network) | |
- Add a new IP address to the Internal Network for Hyper-V attached to the NAT Switch | |
There are also commented commands that you could use to automatically provision machines | |
- Downloads an zipped VM to the local drive | |
- Creates the Virtual Machine in Hyper-V | |
- Issues a Start Command for the new Nested | |
#> | |
Configuration Main | |
{ | |
Param ( | |
[string] $nodeName, | |
[string] $natIpPrefix = "192.168.0", | |
[string[]] $blobs = @() | |
) | |
Import-DscResource -ModuleName 'PSDesiredStateConfiguration', 'xHyper-V' | |
node $nodeName | |
{ | |
# Ensures a VM with default settings | |
xVMSwitch InternalSwitch | |
{ | |
Ensure = 'Present' | |
Name = 'Nat Switch' | |
Type = 'Internal' | |
} | |
Script ConfigureHyperV | |
{ | |
GetScript = | |
{ | |
@{Result = "ConfigureHyperV"} | |
} | |
TestScript = | |
{ | |
return $false | |
} | |
SetScript = | |
{ | |
$natSwitch = Get-NetAdapter -Name "vEthernet (NAT Switch)" | |
New-NetIPAddress -IPAddress "$using:natIpPrefix.1" -PrefixLength 24 -InterfaceIndex $natSwitch.ifIndex | |
New-NetNat -Name NestedVMNATnetwork -InternalIPInterfaceAddressPrefix "$using:natIpPrefix.0/24" -Verbose | |
$size = (Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 2) | |
Resize-Partition -DiskNumber 0 -PartitionNumber 2 -Size $size.SizeMax | |
$ProgressPreference = "SilentlyContinue" | |
Install-Module AzureRm.Storage,AzureRm.Compute -Force | |
$folder = "C:\Images\" | |
New-Item -ItemType Directory -Force -Path $folder | |
foreach ($blob in $using:blobs) | |
{ | |
$filename = $blob.Substring($blob.LastIndexOf("/") + 1) | |
$vhdPath = $folder + $filename | |
Invoke-WebRequest $blob -OutFile $vhdPath | |
$vmName = $filename.SubString(0, $filename.IndexOf(".")) | |
New-VM -Name $vmName -MemoryStartupBytes 1GB -BootDevice VHD -vhdPath $vhdPath -Generation 1 -Switch "NAT Switch" | |
Set-VM -VMname $vmName -AutomaticStartAction Start | |
Start-VM -Name $vmName | |
} | |
# $zipDownload = "http://YOUR-URL-HERE/FILENAME.ZIP" | |
# $downloadedFile = "D:\FILENAME.zip" | |
# $vmFolder = "C:\VM" | |
# Invoke-WebRequest $zipDownload -OutFile $downloadedFile | |
# Add-Type -assembly "system.io.compression.filesystem" | |
# [io.compression.zipfile]::ExtractToDirectory($downloadedFile, $vmFolder) | |
# New-VM -Name VMNAME ` | |
# -MemoryStartupBytes 2GB ` | |
# -BootDevice VHD ` | |
# -VHDPath 'C:\VM\PATH\FILENAME.vhd' ` | |
# -Path 'C:\VM\PATH' ` | |
# -Generation 1 ` | |
# -Switch "NAT Switch" | |
# Start-VM -Name VMNAME | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Azure Nested VV Host Configuration | |
.File Name | |
- InstallHyperV.ps1 | |
.What calls this script? | |
- This is a PowerShell Script run as a Custom Script extention called by azure-deploy.json | |
.What does this script do? | |
- Downloads NuGet package provider | |
- Installs the DscResource and xHyper-V PS modules in support of the upcoming DSC Extenion run in HyperVHostConfig.ps1 | |
- Installs Hyper-V with all Features and Management Tools and then Restarts the Machine | |
#> | |
Set-ExecutionPolicy Unrestricted -Force | |
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | |
Find-Module -Includes DscResource -Name xHyper-v | Install-Module -Force | |
#Install Hyper-V and Reboot | |
Install-WindowsFeature -Name Hyper-V ` | |
-IncludeAllSubFeature ` | |
-IncludeManagementTools ` | |
-Verbose ` | |
-Restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment