I am trying to write a PowerShell script to create an azure AD app and a client secret for that app. In the end: the code should print App-ID, tenant ID, and value of client secret: here is the code that I have written:
# Function to generate a random password
function Generate-Password {
$validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+"
$password = ""
For ($i=0; $i -lt 16; $i++) {
$random = Get-Random -Minimum 0 -Maximum $validChars.Length
$password += $validChars[$random]
}
return $password
}
# Authenticate and login to Azure
Connect-AzAccount
# Set the name, home page URL, and identifier URI of the app
$appName = "test-123"
# Generate a random password for the client secret
$clientSecretPassword = Generate-Password | ConvertTo-SecureString -AsPlainText -Force
# Create the Azure AD app
$app = New-AzADApplication -DisplayName $appName
# Create a client secret for the app
$secret = New-AzADAppCredential -ApplicationId $app.ApplicationId -Password $clientSecretPassword
$secret.Secret
# Print the Client ID and Tenant ID of the app
Write-Host "Client ID: " $app.ApplicationId
Write-Host "Tenant ID: " (Get-AzContext).Tenant.Id
# Print the value of the client secret
$clientsecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret.Secret))
Write-Host "Client Secret: " $clientSecret
but I am getting the following error for this:
Exception calling "SecureStringToBSTR" with "1" argument(s): "Value cannot be null.
Parameter name: s"
At C:\Users\azure-app.ps1:35 char:1
+ $clientsecret = [System.Runtime.InteropServices.Marshal]::PtrToStrin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Client Secret:
Can anyone help me debug this issue or re-factor the code? Thank you
I Tried to reproduce the same in my environment to create an azure AD app and display the client secret & client ID using PowerShell
You can use below PowerShell Script to create Azure AP App with Client Secret.
#Connect to Azure AD
Connect-AzureAD
#Set variables for the app
$appName = "MyApp"
$replyUrls = "http://localhost"
$secret = "MySecret"
#Create the app
$app = New-AzureADApplication -DisplayName $appName -ReplyUrls $replyUrls -PublicClient $false
#Create the client secret
$bytes = [System.Text.Encoding]::Unicode.GetBytes($secret)
$base64 = [System.Convert]::ToBase64String($bytes)
$startDate = Get-Date
$endDate = $startDate.AddYears(1)
$secret = New-AzureADApplicationPasswordCredential -ObjectId $app.ObjectId -CustomKeyIdentifier "MyCustomKeyIdentifier" -Value $base64 -StartDate $startDate -EndDate $endDate
#Retrieve the tenant ID
$tenantId = (Get-AzureADTenantDetail).ObjectId
#Print the App-ID, tenant ID, and client secret
Write-Host "App-ID: $($app.AppId)"
Write-Host "Tenant ID: $tenantId"
Write-Host "Client Secret: $($secret.Value)"
Once ran the above commands Azure App got created successfully with client secret.
When I check Azure App in portal, it got created successfully.