I am writing one script to exclude all SKUs otherthan recommended in policy for that i have written script where it looks all skus and add to policy waiver list if that sku not the file i am fetching, but when i doing PScustomobject its displaying duplicate ones, Anyhelp here is much appreciated
$exclusionsVM1 = @()
$skus = Get-Content ".Dseries.txt"
$subs = Get-AzSubscription
foreach ($sub in $subs) {
Set-AzContext -SubscriptionId $sub.Id -WarningAction Ignore | Out-Null
$vm = Get-AzVm
foreach ($v in $vm) {
if (($v.hardwareprofile.VmSize -match "Standard_D") -and ($v.hardwareprofile.VmSize -notin $skus)) {
$WaiverlistVM1 = [PSCustomObject]@{
VMname = $vm.Name
scope = $vm.Id
} | ConvertTo-Json
}
$exclusionsVM1 += $WaiverlistVM1
}
}
$exclusionsVM | Out-File ".D_waiverlistVM.txt"
$exclusionsVM.count
foreach ($exclusionVM in $exclusionsVM) {
try {
New-AzPolicyExemption -Name $exclusionVM.VMname -PolicyAssignment $Policy -Scope $exclusionVM.scope -ExemptionCategory Waiver
}
catch {
Write-Host "Error while adding VM '$exclusionVM.VMname' to the exclusions" -ForegroundColor Red
}
}