E.C
E.C

Reputation: 15

How to get only part of Powershell output for Azure key vault?

I have this PS script and I'm trying to get user access information for all Key vaults in an Azure subscription.

So what I'm getting now is all the Key vault information; name, location, ids, different settings, network rules, and then access policies for each user that has some access. Is there a way to strip this output to only get the vault name and access policies?

    $resourceGroup = $_ 
    #Gets the key vaults for this resource group
    $keyVaults =  Get-AzKeyVault -ResourceGroupName $resourceGroup

    If ($keyVaults) {
        Write-Host 'Resource Group : ' $resourceGroup
         write-host "`n"
        # Goes through the key vaults for this resource group and shows us the information for each
        Foreach ($keyVault in $keyVaults) {
            Get-AzKeyVault -VaultName $keyVault.VaultName
            write-host "`n"
        }    
    } 
 }

Upvotes: 0

Views: 1157

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25011

You can use Select-Object (alias Select) to narrow down your object's returned properties. Property AccessPolicies returns the Tenant ID value associated with each access policy. Property AccessPoliciesText returns the text of the policies.

Get-AzKeyVault -VaultName $keyVault.VaultName | Select VaultName,AccessPolicies
Get-AzKeyVault -VaultName $keyVault.VaultName | Select VaultName,AccessPoliciesText

Note that since you are selecting only two properties, the default formatted output will be table view. The AccessPoliciesText will likely be truncated in table view. You can (for display purposes only) use list format to display all the contents:

Get-AzKeyVault -VaultName $keyVault.VaultName |
    Select VaultName,AccessPoliciesText | Format-List

Note that if you plan to store your output into a variable to process later, do not use Format-List until you need to display its value at the console.

Upvotes: 2

Related Questions