Azure 自动化无法将参数传递给 VM 内的 powershell 脚本

Azure automation cannot pass parameters to powershell script inside VM

我可以使用 azure automation 运行book(powershell) 运行 驻留在 VM 中的 Powershell。只要 VM Powershell 不需要任何参数,它就可以正常工作。如果我们将 运行book 中的参数发送到 VM powershell,则它无法正常工作。 VM powershell 收到的参数为空。 Runbook 没有其他问题。 参数是这样传递的

$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

这是在 VM-powershell-script 中接收参数的方式

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

经过我的验证,你只需要将一个 Hashtable 变量传递给 运行 命令参数 -Parameter,参考 here.

这里有一个工作示例供您参考:

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