azurepowershellazure-devops

Cannot convert the value of type "System.String" to type "System.Collections.Hashtable"


In Azure Devops I'm trying to run a block of Azure Powershell inline script:

Invoke-AzVMRunCommand -ResourceGroupName xxx-Name xxx -CommandId 'RunPowerShellScript' -ScriptPath $env:SYSTEM_DEFAULTWORKINGDIRECTORY/_alias/xxx.ps1 -Parameter $env:xxx -Verbose

As you can see I am passing a parameter to the xxx.ps1 script. This is a small piece of the content of the xxx.ps1 script:

Param(
[string]$xxx
)

#create AD Group
$ADGroupName = "AD" + $xxx+ "_AD"
.....

When I run this code I get the following error in the logs:

[error]Cannot bind parameter 'Parameter'. Cannot convert the "valueofthevariable" value of type "System.String" to type "System.Collections.Hashtable".

How can I correctly pass the value as parameter? Thanks!


Solution

  • After some more searching online I found my mistake:

    In the Powershell command Invoke-AzVMRunCommand I used a String as input type for Parameter, this has to be a hashtable. I changed that command to the following:

    Invoke-AzVMRunCommand -ResourceGroupName xxx-Name xxx -CommandId 'RunPowerShellScript' -ScriptPath $env:SYSTEM_DEFAULTWORKINGDIRECTORY/_alias/xxx.ps1 -Parameter @{xxx = $env:xxx} -Verbose