Skip to content

Commit d66eb0f

Browse files
committed
Add PowerShell scripts to Key Vault quickstart doc
1 parent abe278b commit d66eb0f

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

articles/key-vault/quick-create-net.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ az group create --name "myResourceGroup" -l "EastUS"
8383
az keyvault create --name <your-unique-keyvault-name> -g "myResourceGroup"
8484
```
8585

86+
```azurepowershell
87+
New-AzResourceGroup -Name myResourceGroup -Location EastUS
88+
89+
New-AzKeyVault -Name <your-unique-keyvault-name> -ResourceGroupName myResourceGroup -Location EastUS
90+
```
91+
8692
### Create a service principal
8793

8894
The simplest way to authenticate a cloud-based .NET application is with a managed identity; see [Use an App Service managed identity to access Azure Key Vault](managed-identity.md) for details. For the sake of simplicity however, this quickstart creates a .NET console application. Authenticating a desktop application with Azure requires the use of a service principal and an access control policy.
@@ -110,14 +116,39 @@ This operation will return a series of key / value pairs.
110116
}
111117
```
112118

119+
Create a service principal using Azure PowerShell [New-AzADServicePrincipal](/powershell/module/az.resources/new-azadserviceprincipal) command:
120+
121+
```azurepowershell
122+
# Create a new service principal
123+
$spn = New-AzADServicePrincipal -DisplayName "http://mySP"
124+
125+
# Get the tenant ID and subscription ID of the service principal
126+
$tenantId = (Get-AzContext).Tenant.Id
127+
$subscriptionId = (Get-AzContext).Subscription.Id
128+
129+
# Get the client ID
130+
$clientId = $spn.ApplicationId
131+
132+
# Get the client Secret
133+
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($spn.Secret)
134+
$clientSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
135+
```
136+
137+
For more details about the service principal with Azure PowerShell, refer to [Create an Azure service principal with Azure PowerShell](/powershell/azure/create-azure-service-principal-azureps).
138+
113139
Take note of the clientId, clientSecret, and tenantId, as we will use them in the following steps.
114140

141+
115142
#### Give the service principal access to your key vault
116143

117144
Create an access policy for your key vault that grants permission to your service principal by passing the clientId to the [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-set-policy) command. Give the service principal get, list, and set permissions for both keys and secrets.
118145

119146
```azurecli
120-
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions delete get list set --key-permissions create decrypt delete encrypt get list unwrapKey wrapKey
147+
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions list get set delete purge
148+
```
149+
150+
```azurepowershell
151+
Set-AzKeyVaultAccessPolicy -VaultName <your-unique-keyvault-name> -ServicePrincipalName <clientId-of-your-service-principal> -PermissionsToSecrets list,get,set,delete,purge
121152
```
122153

123154
#### Set environmental variables
@@ -170,6 +201,10 @@ You can verify that the secret has been set with the [az keyvault secret show](/
170201
az keyvault secret show --vault-name <your-unique-keyvault-name> --name mySecret
171202
```
172203

204+
```azurepowershell
205+
(Get-AzKeyVaultSecret -VaultName <your-unique-keyvault-name> -Name mySecret).SecretValueText
206+
```
207+
173208
### Retrieve a secret
174209

175210
You can now retrieve the previously set value with the [client.GetSecret method](/dotnet/api/microsoft.azure.keyvault.keyvaultclientextensions.getsecretasync).
@@ -190,6 +225,10 @@ You can verify that the secret is gone with the [az keyvault secret show](/cli/a
190225
az keyvault secret show --vault-name <your-unique-keyvault-name> --name mySecret
191226
```
192227

228+
```azurepowershell
229+
(Get-AzKeyVaultSecret -VaultName <your-unique-keyvault-name> -Name mySecret).SecretValueText
230+
```
231+
193232
## Clean up resources
194233

195234
When no longer needed, you can use the Azure CLI or Azure PowerShell to remove your key vault and the corresponding resource group.

0 commit comments

Comments
 (0)