Skip to content

Instantly share code, notes, and snippets.

@dollarpo7
Created June 24, 2020 09:25
Show Gist options
  • Select an option

  • Save dollarpo7/6d16791e15d6529a15fe6c350a10e712 to your computer and use it in GitHub Desktop.

Select an option

Save dollarpo7/6d16791e15d6529a15fe6c350a10e712 to your computer and use it in GitHub Desktop.
DiskSnapshot
#Get service principal details from shared resources
$cred = Get-AutomationPSCredential -Name 'SPCreds'
$tenantId = Get-AutomationVariable -Name 'TenantId'
#Auth with service principal
Connect-AzAccount -ServicePrincipal -Credential $cred -Tenant $tenantId
#Get all Windows VMs in the WebServers resource group
$vms = Get-AzVM -ResourceGroupName webservers |
Where-Object {$_.StorageProfile.OsDisk.OsType -eq 'Windows'}
#Loop over each VMs data disks and cut a snapshot of them
foreach($vm in $vms) {
$vmName = $vm.name
$vm.StorageProfile.DataDisks | ForEach-Object {
$snapConfig = New-AzSnapshotConfig -SourceUri $_.ManagedDisk.id -Location 'westus2' -CreateOption copy
$snap = New-AzSnapshot -Snapshot $snapConfig -SnapshotName "$vmName-$($_.name)-snap-$((Get-Date).ToString('MM-dd-yyyy'))" -ResourceGroupName webservers
Set-AzResource -ResourceId $snap.Id -Tag @{Created=(Get-Date).ToLongDateString()} -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment