Blue Clouds
Blue Clouds

Reputation: 8151

Azure automation cannot pass parameters to powershell script inside VM

I can use azure automation runbook(powershell) to run a Powershell residing in the VM. It works well as long as there is no parameters required by the VM Powershell. If we send parameters from the runbook to VM powershell then it is not working. VM powershell recieves parameters as null. Runbook has no issues otherwise. This is how parameter is passed.

$runcmdparameters=
@{"name" = "EXE"}

Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1

Invoke-AzVMRunCommand -AsJob -ResourceGroupName $RG-Name -Name $myName-CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1 -Parameter $runcmdparameters -Verbose

This is how the parameter is received in the VM-powershell-script

[CmdletBinding()]
param (
    [Parameter(Position=0)]
    [string]$name
)

Upvotes: 1

Views: 947

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28224

After my validation, you just need to pass a Hashtable variable into the run command parameters -Parameter, refer here.

Here is a working sample for your reference:

function Install-Postgres {

[CmdletBinding()]
param (
    [Parameter(Position=0)]
    [string]$name
)
     
Write-Host "This is a sample script with parameters $name"

}

$ScriptToRun = Get-Content Function:\Install-Postgres

Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1

$params = @{"name"="EXE" }

$ss=Invoke-AzVMRunCommand -ResourceGroupName $rgName -VMName $VMVame -ScriptPath "ScriptToRun.ps1" -CommandId 'RunPowerShellScript' -Parameter $params
Write-output $ss.Value[0].Message

enter image description here

Upvotes: 1

Related Questions