Forum Discussion
Need assistance with automating MS Authentication in a PowerShell script
Hello Lorenz33,
In addition to suggestions mentioned by Alex_Rechs, you can also look into 2 extra options:
(1) using ServicePrinciple with ClientSecret
Prerequisites: Register App in AAD and create ClientSecret.
Code to authenticate will be something like this:
$TenantId="[TenantId]"
$ClientId="[YourRegisteredAADAppClientId]"
$ClientSecret="[YourRegisteredAADAppClientSecret]"
$PWord = ConvertTo-SecureString -String $ClientSecret -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ClientId, $PWord
Connect-PowerBIServiceAccount -Tenant $TenantId -ServicePrincipal -Credential $Credential
(2) using ServicePrinciple with Certificate
Prerequisites: Register App in AAD and upload certificate (self signed will suffice).
Authentication code will looks like this:
$AppId= "[YourRegisteredAADAppClientId]"
$Cert="[Thumbprint]"
Connect-PowerBIServiceAccount -ServicePrincipal -CertificateThumbprint $Cert -ApplicationId $AppIdReferences:
- Connect-PowerBIServiceAccount
- Create an Azure Active Directory application and service principal that can access resources
Hope that helps.
- AndySvintsMar 22, 2023Iron Contributor
Hello Lorenz33,
You can look into Microsoft.PowerShell.SecretManagement ( provides a convenient way for a user to store and retrieve secrets), which supports multiple secret vault types. For starters, you can use Microsoft.PowerShell.SecretStore ( Local secure store extension vault).
Hope that helps.