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

时间:2018-12-18 05:05:45

标签: azure powershell azure-automation fileshare runbook

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

有可能吗?如果您知道什么,请在这里写些东西

Runbook 1:创建一个Azure文件共享快照

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

Runbook 2:删除Azure 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()
    }
}

1 个答案:

答案 0 :(得分:0)

示例代码如下,我在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门户中创建的:

enter image description here

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

Runbook中的输出:

enter image description here