Last active
May 31, 2020 17:55
-
-
Save moosebay/22ae1d912a29f39620d52c81bc74a5e0 to your computer and use it in GitHub Desktop.
Create VM From Snapshot
This file contains hidden or 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
$conn = Get-AutomationConnection -Name 'AzureRunAsConnection' | |
Add-AzAccount -ServicePrincipal ` | |
-TenantId $conn.TenantId ` | |
-ApplicationId $conn.ApplicationId ` | |
-CertificateThumbprint $conn.CertificateThumbprint | |
$sub = '4' #subnet number | |
$iteration = '10' # instance number | |
$subscriptionId = 'your_subscription_id' | |
$resourceGroupName = 'Farm' | |
$snapshotName = 'windows_image' | |
$location = 'centralus' | |
$storageType = 'Premium_LRS' | |
$NetworkName = "VNET_name" | |
$SubnetName = "sub$($sub)" #subnet | |
$VMSize = "Standard_DS2_v2" #choose your vm size. This was the best for us | |
$OSCreateOption = "Attach" | |
$vmInitial = "win" | |
$VMName = "$($vmInitial)_$($sub)_$($iteration)" | |
$timestamp = Get-Date -UFormat %s # we are going to use timestamp to make sure there are no conflicts | |
$diskName = "$($vmInitial)_hd_$($sub)_$($iteration)_$($timestamp)" | |
$NICName = "$($vmInitial)_nic_$($sub)_$($iteration)_$($timestamp)" | |
$privateIp = "10.0.$($sub).$($iteration)" | |
Select-AzSubscription -SubscriptionId $SubscriptionId | |
$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName | |
$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id | |
Write-Output "Disk creation started" | |
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName | |
Write-Output "Disk created" | |
$vNet = Get-AzVirtualNetwork -Name $NetworkName | |
$SingleSubnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vNet -Name $SubnetName | |
Write-Output "NIC creation started" | |
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $location -SubnetId $SingleSubnet[0].Id -PrivateIpAddress $privateIp | |
Write-Output "NIC created" | |
Write-Output "Disk State: $($disk.ProvisioningState)" | |
# wait until disk is created | |
while ($null -eq $disk -or $null -eq $disk.Id -or $disk.ProvisioningState -ne "Succeeded") { | |
Start-Sleep -Seconds 1 | |
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $diskName | |
} | |
Write-Output "Disk found" | |
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize | |
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id | |
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -CreateOption $OSCreateOption -Windows -ManagedDiskId $disk.Id | |
$VirtualMachine = Set-AzVMBootDiagnostic -VM $VirtualMachine -Disable | |
Write-Output "Started creating VM: $($VMName)" | |
New-AzVM -ResourceGroupName $ResourceGroupName -Location $location -VM $VirtualMachine | |
Write-Output "Created VM: $($VMName)" |
This file contains hidden or 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
param( | |
[Parameter(Mandatory = $true)] | |
[object]$WebhookData | |
) | |
$data = ConvertFrom-Json -InputObject $WebhookData.RequestBody | |
$iteration = $data.iteration | |
$sub = $data.sub | |
.\Login.ps1 | |
$subscriptionId = 'your_subscription_id' | |
$resourceGroupName = 'Farm' | |
$snapshotName = 'windows_image' | |
$location = 'centralus' | |
$storageType = 'Premium_LRS' | |
$NetworkName = "VNET_name" | |
$SubnetName = "sub$($sub)" #subnet | |
$VMSize = "Standard_DS2_v2" #choose your vm size. This was the best for us | |
$OSCreateOption = "Attach" | |
$vmInitial = "win" | |
$VMName = "$($vmInitial)_$($sub)_$($iteration)" | |
$timestamp = Get-Date -UFormat %s # we are going to use timestamp to make sure there are no conflicts | |
$diskName = "$($vmInitial)_hd_$($sub)_$($iteration)_$($timestamp)" | |
$NICName = "$($vmInitial)_nic_$($sub)_$($iteration)_$($timestamp)" | |
$privateIp = "10.0.$($sub).$($iteration)" | |
Select-AzSubscription -SubscriptionId $SubscriptionId | |
$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName | |
$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id | |
Write-Output "Disk creation started" | |
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName | |
Write-Output "Disk created" | |
$vNet = Get-AzVirtualNetwork -Name $NetworkName | |
$SingleSubnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vNet -Name $SubnetName | |
Write-Output "NIC creation started" | |
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $location -SubnetId $SingleSubnet[0].Id -PrivateIpAddress $privateIp | |
Write-Output "NIC created" | |
Write-Output "Disk State: $($disk.ProvisioningState)" | |
# wait until disk is created | |
while ($null -eq $disk -or $null -eq $disk.Id -or $disk.ProvisioningState -ne "Succeeded") { | |
Start-Sleep -Seconds 1 | |
$disk = Get-AzDisk -ResourceGroupName $resourceGroupName -DiskName $diskName | |
} | |
Write-Output "Disk found" | |
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize | |
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id | |
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -CreateOption $OSCreateOption -Windows -ManagedDiskId $disk.Id | |
$VirtualMachine = Set-AzVMBootDiagnostic -VM $VirtualMachine -Disable | |
Write-Output "Started creating VM: $($VMName)" | |
New-AzVM -ResourceGroupName $ResourceGroupName -Location $location -VM $VirtualMachine | |
Write-Output "Created VM: $($VMName)" |
This file contains hidden or 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
param( | |
[Parameter(Mandatory = $true)] | |
[object]$WebhookData | |
) | |
$data = ConvertFrom-Json -InputObject $WebhookData.RequestBody | |
$iteration = $data.iteration | |
$sub = $data.sub | |
$conn = Get-AutomationConnection -Name 'AzureRunAsConnection' | |
Add-AzAccount -ServicePrincipal ` | |
-TenantId $conn.TenantId ` | |
-ApplicationId $conn.ApplicationId ` | |
-CertificateThumbprint $conn.CertificateThumbprint | |
$subscriptionId = 'cdd99c98-0c19-4690-bc63-6db62c19a3d4' | |
$resourceGroupName = 'Farm' | |
$vmInitial = "win" | |
$VMName = "$($vmInitial)_$($sub)_$($iteration)" | |
Remove-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force | |
.\DeleteIdleNICAndDisk.ps1 |
This file contains hidden or 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
$conn = Get-AutomationConnection -Name 'AzureRunAsConnection' | |
Add-AzAccount -ServicePrincipal ` | |
-TenantId $conn.TenantId ` | |
-ApplicationId $conn.ApplicationId ` | |
-CertificateThumbprint $conn.CertificateThumbprint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment