Skip to content

Commit 474a23f

Browse files
authored
Merge pull request MicrosoftDocs#967 from sptramer/content-update/service-principals
Content update/service principals
2 parents 66671c7 + 82c2337 commit 474a23f

File tree

5 files changed

+138
-6907
lines changed

5 files changed

+138
-6907
lines changed

docs-conceptual/azps-1.3.0/authenticate-azureps.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: sttramer
66
manager: carmonm
77
ms.devlang: powershell
88
ms.topic: conceptual
9-
ms.date: 10/29/2018
9+
ms.date: 02/20/2019
1010
---
1111
# Sign in with Azure PowerShell
1212

@@ -43,18 +43,57 @@ $creds = Get-Credential
4343
Connect-AzAccount -Credential $creds
4444
```
4545

46-
## Sign in with a service principal
46+
## Sign in with a service principal <a name="sp-signin"/>
4747

4848
Service principals are non-interactive Azure accounts. Like other user accounts, their permissions are managed with Azure Active Directory. By granting a service principal only the permissions it needs, your automation scripts stay secure.
4949

5050
To learn how to create a service principal for use with Azure PowerShell, see [Create an Azure service principal with Azure PowerShell](create-azure-service-principal-azureps.md).
5151

5252
To sign in with a service principal, use the `-ServicePrincipal` argument with the `Connect-AzAccount` cmdlet. You'll also need the service principal's application ID,
53-
sign-in credentials, and the tenant ID associate with the service principal. To get the service principal's credentials as the appropriate object, use the [Get-Credential](/powershell/module/microsoft.powershell.security/get-credential) cmdlet. This cmdlet will present a prompt for the service principal user ID and password.
53+
sign-in credentials, and the tenant ID associate with the service principal. How you sign in with a service principal will depend on whether it's configured for password-based or certificate-based authentication.
54+
55+
### Password-based authentication
56+
57+
To get the service principal's credentials as the appropriate object, use the [Get-Credential](/powershell/module/microsoft.powershell.security/get-credential) cmdlet. This cmdlet will present a prompt for a username and password. Use the service principal ID for the username.
5458

5559
```azurepowershell-interactive
5660
$pscredential = Get-Credential
57-
Connect-AzAccount -ServicePrincipal -ApplicationId "http://my-app" -Credential $pscredential -TenantId $tenantid
61+
Connect-AzAccount -ServicePrincipal -Credential $pscredential -TenantId $tenantId
62+
```
63+
64+
### Certificate-based authentication
65+
66+
Certificate-based authentication requires that Azure PowerShell can retrieve information from a local certificate
67+
store based on a certificate thumbprint.
68+
69+
```azurepowershell-interactive
70+
Connect-AzAccount -ServicePrincipal -TenantId $tenantId -CertificateThumbprint <thumbprint>
71+
```
72+
73+
In PowerShell 5, the certificate store can be managed and inspected with the [PKI](/powershell/module/pkiclient) module. For PowerShell 6, the process is more complicated. The following scripts show you how to import an existing certificate into the certificate store accessible by PowerShell.
74+
75+
#### Import a certificate in PowerShell 5
76+
77+
```azurepowershell-interactive
78+
# Import a PFX
79+
$credentials = Get-Credential -Message "Provide PFX private key password"
80+
Import-PfxCertificate -FilePath <path to certificate> -Password $credentials.Password -CertStoreLocation cert:\CurrentUser\My
81+
```
82+
83+
#### Import a certificate in PowerShell 6
84+
85+
```azurepowershell-interactive
86+
# Import a PFX
87+
$storeName = [System.Security.Cryptography.X509Certificates.StoreName]::My
88+
$storeLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
89+
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $storeLocation)
90+
$certPath = <path to certificate>
91+
$credentials = Get-Credential -Message "Provide PFX private key password"
92+
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
93+
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($certPath, $credentials.Password, $flag)
94+
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
95+
$store.Add($Certificate)
96+
$store.Close()
5897
```
5998

6099
## Sign in using a managed identity

0 commit comments

Comments
 (0)