Azure VM PowerShell运行命令未传递参数。

huangapple go评论71阅读模式
英文:

Azure VM PowerShell RUN Command not passing parameters

问题

我正在使用Azure CLI运行命令来调用Windows虚拟机,AZ VM run-command,但无法正确传递参数?

从Azure CLI调用的命令:

az vm run-command invoke \
  --resource-group 'resgrpname' \
  --name 'hostname' \
  --command-id 'RunPowerShellScript' \
  --scripts "C:\Scripts\Start_script.ps1" \
  --parameters "A=B"

当PowerShell脚本运行时,它执行正确,追加到一个文件,但不接受参数。PS1脚本中的代码:

param(
    [string]$SID
)

# 在你的脚本中使用$param1和$param2变量
date | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
$A   | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
" "  | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append

输出文件显示日期已正确追加,但否则为空,没有捕获$A的值?有人见过这种情况吗?我可以尝试的明显事项吗?

我已经尝试过只将命令和参数作为文本内联传递,而不是调用PS1脚本,但invoke无法很好地处理这种情况,而且在多个命令的PowerShell脚本中很快变得难以管理。

英文:

I am making an Azure CLI Run command call to a Windows VM, AZ VM run-command but cannot get the parameters to pass properly?

Command to call from Azure CLI:

az vm run-command invoke \
  --resource-group 'resgrpname' \
  --name 'hostname' \
  --command-id 'RunPowerShellScript' \
  --scripts "C:\Scripts\Start_script.ps1" \
  --parameters "A=B"

When the PowerShell script runs, it executes correctly, appending to a file, but doesn't accept the parameter. Code in the PS1 script:

param(
    [string]$SID
)

# Use the $param1 and $param2 variables in your script
date | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
$A   | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
" "  | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append

The output file shows the date correctly appended, but is otherwise blank, not capturing a value for the $A??

Has anyone seen this before? Anything obvious I can try?

I have tried just passing the commands and parameters inline as text, rather than calling a PS1 script, but invoke doesn't handle that very well, and it would soon get unwieldy in a multi-command PowerShell script

答案1

得分: 0

为了使您的脚本正常工作,您在az vm run-command invoke中传递的参数必须与您的PowerShell脚本中的参数名称匹配。

因此,您可以将参数重命名为$A,或者传递参数为SID = B

英文:

For your script to work, the parameter you pass in az vm run-command invoke has to match the name of the parameter in your Powershell script.

Therefore, you either need to rename the parameter to $A or pass the parameter as SID = B

答案2

得分: 0

以下是翻译好的内容:

输出文件显示日期已正确附加,但除此之外为空,未捕获$A的值。

您正在尝试使用az vm run-command命令将参数$A传递给Powershell脚本。然而,似乎脚本未正确捕获该参数。

要解决此问题,请在az vm run-command命令中删除--parameters参数,并将参数从**$A**更改为如下所示的$SID:

SID='B'"

这是更新后的脚本。

Start_script

param(
    [string]$SID
)
date | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
$SID | Out-File -FilePath C:\Scripts\PowerShellLog.txt -Append
" " | Out-File -FilePath C:\Scripts\PowerShellLog.txt -Append

从Azure CLI调用的命令:

az vm run-command invoke --resource-group Aks-rg-test --name venkat-vm --command-id 'RunPowerShellScript' --scripts "C:\Scripts\Start_script.ps1 SID='B'"

输出:

Azure VM PowerShell运行命令未传递参数。

执行上述命令后,SID参数的值将存储在日志文件中。

Azure VM PowerShell运行命令未传递参数。

英文:

> The output file shows the date correctly appended, but is otherwise blank, not capturing a value for the $A??

You're trying to pass a parameter $A to a powershell script using the az vm run-command command. However, it seems that the parameter is not being captured correctly in the script.

To fix this issue, remove the --parameters argument in the az vm run-command command, and also change the parameter from $A to $SID as shown below

   SID='B'"

Here is the update script.

Start_script

param(
    [string]$SID
)
date | Out-File -FilePath C:\scripts\PowerShellLog.txt -Append
$SID | Out-File -FilePath C:\Scripts\PowerShellLog.txt -Append
" "  | Out-File -FilePath C:\Scripts\PowerShellLog.txt -Append```

Command to call from Azure CLI:

 az vm run-command invoke --resource-group Aks-rg-test --name venkat-vm --command-id 'RunPowerShellScript' --scripts "C:\Scripts\Start_script.ps1  SID='B'"

Output:

Azure VM PowerShell运行命令未传递参数。

Once the above command is executed, the value of the SID parameter will be stored in the log file.

Azure VM PowerShell运行命令未传递参数。

huangapple
  • 本文由 发表于 2023年6月9日 10:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定