My requirement is to delete snapshots that are older than 7 days with the name like appname-. but before deleting i need to fetch the snapshot data with some names. the script executes and provides some snapshot datas inidtially but in middle thhows below error. could someone help me on this.why i got this error message and how to fix
=========================================================================
Get-AzSnapshot : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ResourceGroupName'. Specified method is not supported. At line:6 char:39 + Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotNam ... + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-AzSnapshot], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.Compute.Automation.GetAzureRmSnapshot
$snapshotnames = (Get-AzSnapshot).Name
foreach($snapname in $snapshotnames)
{
$resourceGroupName = (Get-AzResource -Name $snapname).ResourceGroupName
Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapname | Where-Object {($_.TimeCreated -eq (Get-Date).AddDays(-3))} <#-and ($_.Name -like '*-2019_*') } | select Name,TimeCreated -Verbose
}
Get-AzResource -Name $snapname
ever result in multiple items? If that happens, then$resourceGroupName
will become an object array. Then you will have this problem. – AdminOfThings