1

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
)
0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't look any different to the originally posted code so I guess there must be some other issue
Actually it does. The script(function) is part of automation script. While the OP had put that in the VM as a powershell file. Passing parameters to that caused issue. This is not solving that problem but this is a better solution for the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.