I'm running the below lines of my script:
Write-Host "Creating KeyVault..."
az keyvault create --name mykeyvault --resource-group myrg --location polandcentral
Write-Host "Creating service principal..."
az ad sp create-for-rbac --name mysp --role contributor --scopes "/subscriptions/subscriptionid/resourceGroups/myrg" --create-cert --cert mycert --keyvault mykeyvault
But the effect is that a Keyvault is created as well as service principal. However those last three options of az ad sp create-for-rbac
command doesn't seem to do anything, because:
When I browse my Keyvault in the portal, I can see it's empty
The output from the sp creation command is (and I would expect something different, since I'm making it use certificate created in KV):
{ "appId": "...", "displayName": "mysp", "password": "...", "tenant": "..." }
What is it that I'm doing wrong here?
EDIT
The az ad sp create-for-rbac
command returns this output:
Found an existing application instance: (id) <here goes the guid>. We will patch it.
Creating 'contributor' role assignment under scope '/subscriptions/<sub_guid>'
Role assignment already exists.
In my case, I connected to Azure via CLI using below command by signing in with
Sri
user:Response:
When I ran your code in my environment, certificate created successfully in key vault along with service principal as below:
Response:
To confirm that, you can also run below CLI command that retrieves certificates present in specific key vault:
Response:
When I tried to check the same from different user account, I got below screen with no certificates:
To resolve this, you need to add new access policy in key vault by enabling
List
access for certificates to other user too like this:When I tried to check the same from that user account again, I can find the certificate successfully in key vault as below: