getting "ERROR: Cannot bind argument to parameter 'String' because it is null.
i am passing clientSecret as an environment variable but looks like powershell function app is not reading this. i pass it in an azure pipeline.
param($Timer)
Import-Module SqlServer
$currentUTCtime = (Get-Date).ToUniversalTime()
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Define service principal
$tenantId = "tenantId "
$clientId = "clientId"
$clientSecret = ConvertTo-SecureString $env:CLIENTSECRET -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret)
# Analysis Services details
$envUpper = $env:ENVUPPER
$env = $envUpper.ToLower()
$analysisServicesServerPrefix = "ServerPrefix"
$fullServerName = $analysisServicesServerPrefix + $env
$analysisServicesDatabase = "analysisServicesDatabase"
try {
# Authenticate with service principal
Connect-AzAccount -ServicePrincipal -Credential $cred -Tenant $tenantId
Write-Host "Successfully authenticated with the service principal."
} catch {
# Write the error message and throw error for authentication failure
Write-Error "Error occurred during authentication: $_"
throw $_
}
try {
# Refresh the data model using Invoke-ProcessASDatabase
Invoke-ProcessASDatabase -Server $fullServerName -DatabaseName $analysisServicesDatabase -RefreshType "Full"
Write-Host "Successfully refreshed the data model."
} catch {
# Write the error message and throw error for data model refresh failure
Write-Error "Error occurred during data model refresh: $_"
throw $_
}
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
Release pipeline (for security just replaced my actual CLIENTSECRET with my-client-secret for this question)
[![enter image description here][1]][1]
I am correctly using environment variable the method is just
$clientSecret = $env:CLIENTSECRET
full error log
[![enter image description here][2]][2]
ERROR: Error occurred during authentication: Cannot bind argument to parameter 'Credential' because it is null. Exception : Type : Microsoft.PowerShell.Commands.WriteErrorException Message : Error occurred during authentication: Cannot bind argument to parameter 'Credential' because it is null.
From my understanding and looking at the logs I assume that clientSecret is being passed as null and not the actual the value causing the error? this must mean its due to environment variable not being read.
If I did not misunderstand, you actually set the 'CLIENTSECRET
' as a secret variable on the release pipeline.
Unlike a general pipeline variable, by default, the secret variable will not be automatically mapped as an environment variable on the agent.
In this situation, if you want to call use the environment variable of a secret variable in script, you need to explicitly map this secret variable as an environment variable.
For more details, you can reference this post.
EDIT:
Before the Azure Function App task, you can add a PowerShell task like as below image shows. For your PowerShell function, you do not need to change anything.