【问题标题】:Azure automation cannot pass parameters to powershell script inside VMAzure 自动化无法将参数传递给 VM 内的 powershell 脚本
【发布时间】:2021-03-21 20:21:39
【问题描述】:

我可以使用 azure automation runbook(powershell) 来运行驻留在 VM 中的 Powershell。只要 VM Powershell 不需要任何参数,它就可以正常工作。如果我们将参数从运行手册发送到 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
)

【问题讨论】:

    标签: azure powershell azure-automation


    【解决方案1】:

    经过我的验证,你只需要将一个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
    

    【讨论】:

    • 这看起来与最初发布的代码没有什么不同,所以我想肯定有其他问题
    • 确实如此。脚本(函数)是自动化脚本的一部分。虽然 OP 已将其作为 powershell 文件放入 VM 中。将参数传递给导致问题。这并不能解决这个问题,但对于 OP 来说这是一个更好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 2011-08-01
    • 2015-06-07
    • 2017-05-08
    • 2019-07-07
    • 2013-04-27
    • 2012-12-10
    相关资源
    最近更新 更多