3

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
3
  • You're trying to compare two string values using -lt, this isn't going to work in the way you want. Try removing .ToString() and you might get a better result comparing the native datetime objects instead :) Sep 20, 2018 at 16:29
  • like this: ?{($_.TimeCreated) -lt ([datetime]::Today.AddDays(-1))} | remove-azurermsnapshot -force Sep 20, 2018 at 16:57
  • I ran this and nothing was deleted. There is one vm in the said resource group that was created today (9/20/2018) $rg = 'snapshots' $snapshotnames = (Get-AzureRmSnapshot -ResourceGroupName $rg).name foreach($snapname in $snapshotnames) { Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname |?{($_.TimeCreated) -lt ([datetime]::Today.AddDays(-1))} | remove-azurermsnapshot -force | remove-azurermsnapshot -force } Sep 20, 2018 at 17:03

1 Answer 1

1

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

enter image description here

My snapshot:

enter image description here

Also check the logs in the portal:

enter image description here

Here is a similar issue, refer to this link.

1
  • Thanks this worked for me. I took off the .tostring and formating the time portion off. Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname | ?{($_.TimeCreated) -lt ([datetime]::Now.AddDays(0))} #| remove-azurermsnapshot -force this was my code section that works, thank you and thanks to @James C for your help. Sep 21, 2018 at 13:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.