本文中包含两个脚本,一个是删除托管磁盘快照的脚本,一个是使用托管磁盘快照还原磁盘的脚本。
比较简单,只是几个命令的调用,这里就不详细解释具体的语句意义了,大家直接看脚本吧:
删除快照的脚本:
param(
[Parameter(Mandatory = $true)]
[string]$SubscriptionName,
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ManagedDiskName
)
[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);
$disk = Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $ManagedDiskName;
if ($disk -eq $null)
{
Write-Host "Disk not found";
return;
}
$snapshots = Get-AzureRmSnapshot;
$filteredSnapshots = @($snapshots | where {$_.CreationData.SourceUri -eq $disk.Id});
Write-Host ("Snapshot found for disk {0}:" -f $ManagedDiskName) -ForegroundColor Yellow;
$filteredSnapshots | select @{Name="Id"; Expression={$filteredSnapshots.IndexOf($_) + 1}}, @{Name="TimeCreated"; Expression={$_.TimeCreated.ToLocalTime()}}, Name | ft;
$selection = (Read-Host "Please select the snapshot you want to delete, input 'a' if you want to delete all snapshots");
If ($PSCmdlet.ShouldContinue("Confirm?", "=== Confirm delete Opeartion ==="))
{
if ($selection -eq "a")
{
foreach ($snapshot in $filteredSnapshots)
{
Write-Host ("Deleting snapshot {0}" -f $snapshot.Name) -ForegroundColor Yellow;
[void](Remove-AzureRmSnapshot -ResourceGroupName $snapshot.ResourceGroupName -SnapshotName $snapshot.Name -Force);
Write-Host ("Snapshot {0} deleted" -f $snapshot.Name) -ForegroundColor Green;
}
}
else
{
$selection = $selection - 1;
$snapshot = $filteredSnapshots[$selection];
Write-Host ("Deleting snapshot {0}" -f $snapshot.Name) -ForegroundColor Yellow;
[void](Remove-AzureRmSnapshot -ResourceGroupName $snapshot.ResourceGroupName -SnapshotName $snapshot.Name -Force);
Write-Host ("Snapshot {0} deleted" -f $snapshot.Name) -ForegroundColor Green;
}
}
还原磁盘的脚本:
param(
[Parameter(Mandatory = $true)]
[string]$SubscriptionName,
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ManagedDiskName,
[Parameter(Mandatory = $true)]
[string]$RestoredDiskName
)
[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);
$disk = Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $ManagedDiskName;
if ($disk -eq $null)
{
Write-Host "Disk not found";
return;
}
$snapshots = Get-AzureRmSnapshot;
$filteredSnapshots = @($snapshots | where {$_.CreationData.SourceUri -eq $disk.Id});
Write-Host ("Snapshot found for disk {0}:" -f $ManagedDiskName) -ForegroundColor Yellow;
$filteredSnapshots | select @{Name="Id"; Expression={$filteredSnapshots.IndexOf($_) + 1}}, @{Name="TimeCreated"; Expression={$_.TimeCreated.ToLocalTime()}}, Name | ft;
$selection = (Read-Host "Please select the snapshot you want to restore");
$selection = $selection - 1;
$snapshot = $filteredSnapshots[$selection];
$restoredDiskConfig = $null;
if ($snapshot.OsType -eq $null)
{
$restoredDiskConfig = New-AzureRmDiskConfig -AccountType $snapshot.AccountType -Location $snapshot.Location -CreateOption Copy -SourceResourceId $snapshot.Id;
}
else
{
$restoredDiskConfig = New-AzureRmDiskConfig -AccountType $snapshot.AccountType -OsType $snapshot.OsType -Location $snapshot.Location -CreateOption Copy -SourceResourceId $snapshot.Id;
}
Write-Host ("Restoring data to managed disk {0}" -f $RestoredDiskName) -ForegroundColor Yellow;
[void](New-AzureRmDisk -DiskName $RestoredDiskName -Disk $restoredDiskConfig -ResourceGroupName $ResourceGroupName);
Write-Host "Finished" -ForegroundColor Yellow;
脚本调用示例:
删除:
还原: