Passing parameter to script from Invoke-AzVMRunCommand

半城伤御伤魂 提交于 2021-01-29 05:18:35

问题


I trying to execute the Invoke-AzVMRunCommand to execute a PS function that accept parameters. The code below shows call to the Invoke-AzVMRunCommand

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

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

The PS Script that I need to execute on the server is

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.

Microsoft documentation on Invoke-AzVMRunCommand is not very helpful.

How can I pass the parameter to the script?


回答1:


I think you need to do this:

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

Because what happens now: your script doesnt really accept parameters, you function does, but you are calling the script, not the function. and then you call the function inside the script. another way of fixing this - add parameters to the script itself



来源:https://stackoverflow.com/questions/54479813/passing-parameter-to-script-from-invoke-azvmruncommand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!