Skip to content

Commit eae985c

Browse files
committed
Added code to obtain service principal secret
1 parent 9e06fe6 commit eae985c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

docs-conceptual/azps-2.3.2/create-azure-service-principal-azureps.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ Without any other authentication parameters, password-based authentication is us
4242
$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName
4343
```
4444

45-
The returned object contains the `Secret` member, which is a `SecureString` containing the generated password. Make sure that you store this value somewhere secure to authenticate with the service principal. Its value __won't__ be displayed in the console output. If you lose the password, [reset the service principal credentials](#reset-credentials).
45+
The returned object contains the `Secret` member, which is a `SecureString` containing the generated password. Make sure that you store this value somewhere secure to authenticate with the service principal. Its value __won't__ be displayed in the console output. If you lose the password, [reset the service principal credentials](#reset-credentials).
46+
47+
The following code will allow you to export the secret:
48+
49+
```azurepowershell-interactive
50+
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sp.Secret)
51+
$UnsecureSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
52+
```
4653

4754
For user-supplied passwords, the `-PasswordCredential` argument takes `Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential` objects. These objects must have a valid `StartDate` and `EndDate`, and take a plaintext `Password`. When creating a password, make sure you follow the [Azure Active Directory password rules and restrictions](/azure/active-directory/active-directory-passwords-policy). Don't use a weak password or reuse a password.
4855

0 commit comments

Comments
 (0)