从 Invoke-AzVMRunCommand 向脚本传递参数

Passing parameter to script from Invoke-AzVMRunCommand

我正在尝试执行 Invoke-AzVMRunCommand 以执行接受参数的 PS 函数。下面的代码显示了对 Invoke-AzVMRunCommand

的调用
$runcmdparameters=@{
        "VolumeLable"="sdsd";
        "azurelun"="1,3,4"
                    }

Invoke-AzVMRunCommand -ResourceGroupName $ServerResourceGroupName -VMName $VMVame -ScriptPath "c:\Configurestorage.ps1" -CommandId 'RunPowerShellScript' -Parameter $runcmdparameters -Verbose

我需要在服务器上执行的PS脚本是

function Configure-Storage
     {
          Param(
          [parameter(Mandatory=$true)][String]$VolumeLable,
          [parameter(Mandatory=$true)][String[]]$azurelun
          )

#create a storage pool for user databases.
 Out-File "C:\Temp\log.txt" -InputObject $VolumeLable -Append

}

Configure-Storage -VolumeLable $VolumeLable -azurelun $azurelun

The script fail with Cannot bind argument to parameter 'VolumeLable' because it is an empty string.

关于 Invoke-AzVMRunCommand 的 Microsoft 文档不是很有帮助。

如何将参数传递给脚本?

我认为你需要这样做:

Param(
   [parameter(Mandatory=$true)][String]$VolumeLable,
   [parameter(Mandatory=$true)][String[]]$azurelun
)

#create a storage pool for user databases.
Out-File "C:\Temp\log.txt" -InputObject $VolumeLable -Append

因为现在发生了什么:你的脚本并没有真正接受参数,你的函数接受了,但你调用的是脚本,而不是函数。然后你调用脚本中的函数。解决此问题的另一种方法 - 将参数添加到脚本本身