Deploying an AzureKeyVault and trying to retrieve ObjectId with an inline powershell script

Josh Martin 5 Reputation points
2023-02-18T07:21:09.1233333+00:00

ArmTemplateCode.pdf

Hi guys, I'm thinking this is the best place to post this as you guys are the cream of the crop. I'm very new to azure Devops and I am playing around ARM templates and have created a relatively simple azurekeyvault. In my code I am trying to create an inline PowerShell script that will grab someone deploying the ARM templates ObjectId and storing it into the parameter, as opposed to entering it in manually or having to deploy it from the azure portal. I just cannot seem to get it to work, and it is quite frustrating. I was wondering if someone could take a quick look and maybe explain what I am doing wrong. Very Kind regards.

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,453 questions
Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Syed Shiraz Shahid 290 Reputation points
    2023-03-15T20:59:03.0766667+00:00

    To retrieve the ObjectId of an Azure Key Vault, you can use the Azure PowerShell module to run a script inline. Here's an example of how to do it:

    1. Open PowerShell and log in to your Azure account using the Connect-AzAccount cmdlet.
    2. Create a new Key Vault using the New-AzKeyVault cmdlet. Make sure to include the -EnabledForDeployment parameter, as this will allow you to retrieve the ObjectId later on.
        powershellCopy code
        $resourceGroup = "myResourceGroup"
    $vaultName = "myKeyVault"
    
    New-AzKeyVault -ResourceGroupName $resourceGroup -Name $vaultName -EnabledForDeployment
        ```
        
        
    1. Retrieve the ObjectId of the Key Vault using the **`Get-AzResource`** cmdlet. This cmdlet returns an Azure Resource Manager resource, which includes the ObjectId of the Key Vault.
        
        
    
    powershellCopy code
    $keyVault = Get-AzResource -ResourceGroupName $resourceGroup -ResourceType "Microsoft.KeyVault/vaults" -ResourceName $vaultName
    

    $keyVault.ObjectId ```

    The **`Get-AzResource`** cmdlet is used to retrieve the Key Vault resource, and the **`ObjectId`** property is used to retrieve the ObjectId of the Key Vault.
    

    That's it! You can now use this ObjectId in other scripts and commands to manage your Key Vault.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.