It looks like the issue might be related to how the client secret is being read and used in your script. Here are a few things to check and try:
- Ensure the Client Secret is Correct:
- Verify that the client secret stored in
C:\temp\key.txt
is correct and hasn’t expired. You can do this by generating a new client secret in the Azure portal and updating your text file.
- Verify that the client secret stored in
- Convert the Client Secret Properly:
- When reading the client secret from the file, ensure it is being converted correctly. You might need to use
ConvertTo-SecureString
with the-AsPlainText
and-Force
parameters if the secret is stored as plain text.- Use the Correct Parameters:
- Update your script to ensure the client secret is being passed correctly. Here is an example of how you might modify your script: $ClientId = "111111111111111111"
`$ClientSecret = Get-Content "C:\temp\key.txt" | ConvertTo-SecureString -AsPlainText -Force` `$TenantId = "2222222222222222"` `$body = @{` ` client_id = $ClientId` ` client_secret = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($ClientSecret))` ` tenant_id = $TenantId` ` grant_type = "client_credentials"` `}`
- Update your script to ensure the client secret is being passed correctly. Here is an example of how you might modify your script: $ClientId = "111111111111111111"
- Use the Correct Parameters:
- Check for Typos:
- Ensure there are no typos in your
ClientId
,ClientSecret
, orTenantId
.
- Ensure there are no typos in your
- Review Azure Documentation:
- Refer to the Azure documentation for detailed steps on registering an app and using client credentials.