是否可以使用 Azure 自动化 Runbook 删除另一个 Runbook 输出(Azure 文件共享快照)?

Is it possible to use Azure Automation Runbook to delete another Runbook output (an Azure File share snapshot)?

我想使用 Runbook 删除另一个 Runbook 输出(Azure 文件共享快照)。

可能吗?如果你知道什么,请在这里写点东西

操作手册 1:创建 Azure 文件共享快照

$context = New-AzureStorageContext -StorageAccountName -StorageAccountKey 
$share = Get-AzureStorageShare -Context 
$context -Name "sharefile" 
$snapshot = $share.Snapshot()

Runbook 2:删除 Azure Runbook 输出。这样做的问题是它会删除所有快照,而不是只删除第一个 runbook 创建的快照。

$allsnapshots = Get-AzureStorageShare -Context $context | Where-Object { $_.Name -eq "sharefile" -and $_.IsSnapshot -eq $true } 
foreach($snapshot in $allsnapshots){ 
    if($snapshot.SnapshotTime -lt (get-date).Add·Hours()){ 
        $snapshot.Delete()
    }
}

示例代码如下,我在runbook中测试,效果很好(创建快照,3分钟后删除),其他快照无效.

我的 powershell runbook 中的代码:

param(
[string]$username,
[string]$password,
[string]$filesharename
)

$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
$share = Get-AzureStorageShare -Context $context -Name $filesharename
$s = $share.snapshot()

#get the snapshot name, which is always a UTC time formated value
$s2= $s.SnapshotQualifiedStorageUri.PrimaryUri.ToString()

#the $snapshottime is actually equal to snapshot name
$snapshottime = $s2.Substring($s2.IndexOf('=')+1)
write-output "create a snapshot"
write-output $snapshottime

#wait 180 seconds, then delete the snapshot 
start-sleep -s 180

write-output "delete the snapshot"
$snap = Get-AzureStorageShare -Context $context -SnapshotTime $snapshottime -Name $filesharename 
$snap.Delete()

write-output "deleted successfully after 3 minutes"

在运行之后,可以看到在azure portal中创建了快照:

完成后,指定的快照被删除(由于某些缓存问题,您可能需要打开一个新网页才能看到更改)

Runbook 中的输出: