本文介绍了如何每10分钟删除一次托管磁盘快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本正在使用,以便删除早于​​10分钟的快照并保留不早于10分钟的快照,我有以下脚本,但它无法正常工作,任何人都可以告诉我怎么了?

I have the below script which I am using in order to delete snapshot older then 10 minutes and retain the snapshot that are not older then 10minutes, I have the below script but its not working as it suppose to, can anyone tell me whats being going wrong?

foreach($snapname in $snapshotnames)
{
    Get-AzureRmSnapshot -ResourceGroupName $rg  -SnapshotName $snapname |?{$_.Name -Like "*-Server1*"} | ?{($_.TimeCreated).ToString('yyyyMMdd') -lt ([datetime]::Now.AddMinutes(-10).tostring('yyyymmdd'))} | remove-azurermsnapshot -force
}

推荐答案

您应该使用 [datetime] :: UtcNow 而不是 [datetime] :: Now 使用 .tostring('yyyymmdd').

You should use [datetime]::UtcNow instead of [datetime]::Now and not use .tostring('yyyymmdd').

所以您的命令应该是:

foreach($snapname in $snapshotnames)
{
    Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname | ?{$_.Name -Like "*-Server1*"} | ?{($_.TimeCreated) -lt ([datetime]::UtcNow.AddMinutes(-10))} | remove-azurermsnapshot -force
}

我的特定测试命令:

Get-AzureRmSnapshot -ResourceGroupName "<ResourceGroupName>" -SnapshotName "<SnapshotName>" | ?{($_.TimeCreated) -lt ([datetime]::UtcNow.AddMinutes(-10))} | remove-azurermsnapshot -force

结果截图:

这篇关于如何每10分钟删除一次托管磁盘快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 16:49