Forum Discussion
shresthasaluja
Oct 10, 2022Copper Contributor
foreach loop not working
$result = @() $result += "KeyVaultCertName, KeyVaultName, KeyVaultCertExpiryDate" $KEYVAULTS=$(az keyvault list --query "[].name") foreach($KEYVAULT in $KEYVAULTS) { $AllKeyVaultCerts = $(az key...
LainRobertson
Oct 10, 2022Silver Contributor
You might find the Microsoft Azure Resource Manager module a little easier to use:
Here's the sample script that uses it.
$ExpiryThreshold = [datetime]::UtcNow.AddDays(50);
Get-AzKeyVault |
ForEach-Object {
$Vault = $_;
Get-AzKeyVaultCertificate -VaultName ($Vault.VaultName) |
Where-Object {
$_.Expires -lt $ExpiryThreshold;
} |
ForEach-Object {
[PSCustomObject] @{
VaultName = $Vault.VaultName;
Name = $_.Name;
Enabled = $_.Enabled;
Expires = $_.Expires.ToString("u");
}
}
}
Which produces this output (I had to increase the expiry threshold given the dates were a year from now, but we're focusing on the formatting here, not the values):
You can add Export-Csv to pipe that to a CSV file if you so desire.
I can't really speak to your error. It may be accurate or it may not - I cannot discern one way or another.
Cheers,
Lain