Reputation: 55
I am close to what I need but I am missing something because the snapshot isn't removing. I think its the datetime string I have isn't all the way correct, so I am here for a little help. Here is my current PS code.
rg = 'snapshots'
$snapshotnames = (Get-AzureRmSnapshot -ResourceGroupName $rg).name
foreach($snapname in $snapshotnames)
{
Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname |
?{($_.TimeCreated).ToString('yyyy-MM-dd') -lt ([datetime]::Today.AddDays(-1).tostring('yyyy-MM-dd'))} |
remove-azurermsnapshot -force
}
The name of the snapshot is formatted like this with the below code: Testvm---2018-09-20
$timestamp = Get-Date -f ---yyyy-MM-dd
$snapshotName = $vmInfo.Name + $timestamp
So I think my problem area is this part
?{($_.TimeCreated).ToString('yyyy-MM-dd') -lt ([datetime]::Today.AddDays(-1).tostring('yyyy-MM-dd'))} | remove-azurermsnapshot -force
Upvotes: 3
Views: 4213
Reputation: 42053
Try the command below, the sample delete snapshots older then 10
days.
rg = 'snapshots'
$snapshotnames = (Get-AzureRmSnapshot -ResourceGroupName $rg).name
foreach($snapname in $snapshotnames)
{
Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname | ?{($_.TimeCreated) -lt ([datetime]::UtcNow.AddDays(-10))} | remove-azurermsnapshot -force
}
My specify test command:
Get-AzureRmSnapshot -ResourceGroupName joywebapp -SnapshotName joytestss1 | ?{($_.TimeCreated) -lt ([datetime]::UtcNow.AddDays(-10))} | remove-azurermsnapshot -force
My snapshot:
Also check the logs in the portal:
Here is a similar issue, refer to this link.
Upvotes: 1