Functions/Get-KeyVault.ps1
function Get-KeyVault{ try{ $keyVaultItems = New-Object System.Collections.ArrayList; $keyVaults = Get-AzureRmKeyVault -ResourceGroupName $resourceGroupName -ErrorAction Continue; if(!$?){ Out-Error $Global:Error[0]; } Add-Log "resource group being processed: $resourceGroupName" $noOfKeyVaultsFound = $keyVaults.Count; $noOfKeyVaultsProcessed = 0; foreach($keyVault in $keyVaults){ $currentVaultName = $keyVault.VaultName; $currentVaultKeys = Get-AzureKeyVaultKey -VaultName $currentVaultName -ErrorAction Continue; if(!$?){ Out-Error $Global:Error[0]; } foreach($currentVaultKey in $currentVaultKeys){ $currentVaultProcessedKey = New-Object PSObject; $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "VaultName" -Value $currentVaultName $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "ItemName" -Value $currentVaultKey.Name $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "ItemId" -Value $currentVaultKey.Id $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "CreatedDate" -Value $currentVaultKey.Created $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "ExpiryDate" -Value $currentVaultKey.Expires $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "Enabled" -Value $currentVaultKey.Enabled $currentVaultProcessedKey | add-member -MemberType NoteProperty -Name "ItemType" -Value "Key" $keyVaultItems.Add($currentVaultProcessedKey) | Out-Null; } $currentVaultSecrets = Get-AzureKeyVaultSecret -VaultName $currentVaultName -ErrorAction Continue; if(!$?){ Out-Error $Global:Error[0]; } foreach($currentVaultSecret in $currentVaultSecrets){ $currentVaultProcessedSecret = New-Object PSObject; $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "VaultName" -Value $currentVaultName $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "ItemName" -Value $currentVaultSecret.Name $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "ItemId" -Value $currentVaultSecret.Id $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "CreatedDate" -Value $currentVaultSecret.Created $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "ExpiryDate" -Value $currentVaultSecret.Expires $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "Enabled" -Value $currentVaultSecret.Enabled $currentVaultProcessedSecret | add-member -MemberType NoteProperty -Name "ItemType" -Value "Secret" $keyVaultItems.Add($currentVaultProcessedSecret) | Out-Null; } $currentVaultCertificates = Get-AzureKeyVaultCertificate -VaultName $currentVaultName -ErrorAction Continue; if(!$?){ Out-Error $Global:Error[0]; } foreach($currentVaultCertificate in $currentVaultCertificates){ $currentVaultProcessedCert = New-Object PSObject; $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "VaultName" -Value $currentVaultName $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "ItemName" -Value $currentVaultCertificate.Name $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "ItemId" -Value $currentVaultCertificate.Id $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "CreatedDate" -Value $currentVaultCertificate.Created $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "ExpiryDate" -Value $currentVaultCertificate.Expires $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "Enabled" -Value $currentVaultCertificate.Enabled $currentVaultProcessedCert | add-member -MemberType NoteProperty -Name "ItemType" -Value "Certificate" $keyVaultItems.Add($currentVaultProcessedCert) | Out-Null; } $noOfKeyVaultsProcessed++; } if($keyVaultItems){ Set-Output -outputData $keyVaultItems -outputFileName "KeyVault"; } } catch { Out-Error $Global:Error[0]; } } |