Cloud can be tricky sometimes. Find out what scenarios we've ran into that are worth being mentioned and explained.
param location string = resourceGroup().location
resource automationAccount 'Microsoft.Automation/automationAccounts@2019-06-01' = {
name: 'CostOptimizationAutomation'
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
sku: {
name: 'Free'
}
}
}
PowerShell:
$exclusiontag = "ExcludeCostOptimization"
Connect-AzAccount -Identity
$subscriptions = Get-AzSubscription
foreach($subscription in $subscriptions) {
$pips = Get-AzResource -Tag @{$exclusiontag="false"} -Resourcetype "Microsoft.Network/publicIPAddresses"
foreach($pip in $pips){
get-azpublicIPAddress -name $pip.name | Where-Object IpConfiguration -eq $null | Remove-AzPublicIpAddress -Force
}
$snapshots = Get-AzResource -Tag @{$exclusiontag="false"} -Resourcetype "Microsoft.Compute/Snapshots"
$date = (Get-Date).AddDays(-60)
foreach($snapshot in $snapshots) {
Get-AzSnapshot -SnapshotName $snapshot.name | Where-Object { $_.TimeCreated -lt $date } | Remove-AzSnapshot -Force
}
$vms = Get-AzResource -Tag @{$exclusiontag="false"} -Resourcetype "Microsoft.Compute/VirtualMachines"
foreach($vm in $vms) {
get-azvm -Status -Name $vm.Name | Where-Object PowerState -eq "VM stopped" | Stop-AzVM -Force
}
}