forked from CloudSkills/azure-automation-runbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataDiskSnapshot.ps1
22 lines (17 loc) · 949 Bytes
/
DataDiskSnapshot.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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
}
}