简体   繁体   English

在 .ps1 脚本和 .ps1 脚本文件中使用 az vm run-command

[英]Using az vm run-command from within a .ps1 script and with .ps1 script file

I have created a VM in Azure, and I can run the following command from my local Windows 10 Powershell window:我已经在 Azure 中创建了一个 VM,并且可以从我的本地 Windows 10 Powershell 窗口运行以下命令:

az vm run-command invoke --command-id RunPowerShellScript --resource-group $ResourceGroup --name $AD1Name --scripts "@provision.ps1"

But when I put this line in a Powershell script file, CreateVMAndProvision.ps1, and run my CreateVMAndProvision.ps1 that also run the az vm create... I get the following error:但是,当我将此行放在 Powershell 脚本文件 CreateVMAndProvision.ps1 中,并运行我的 CreateVMAndProvision.ps1 并运行az vm create...我收到以下错误:

  "value": [
    {
      "code": "ComponentStatus/StdOut/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "",
      "time": null
    },
    {
      "code": "ComponentStatus/StdErr/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "At C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\1.1.5\\Downloads\\script6.ps1:1 char:1
+ @provision.ps1
+ ~~~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@provision'
can be used only as an argument to a command. To reference variables in an expression use
'$provision'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted
 ",
      "time": null
    }
  ]
}

The syntax of specifying my local provision.ps1 file to the az cli command as "@provision.ps1" seem to be the problem, but I can not figure out how to solve this.将 az cli 命令的本地 provision.ps1 文件指定为"@provision.ps1"的语法似乎是问题所在,但我不知道如何解决这个问题。 I have tried to use double quotes, combination of double and single quotes, here string (@"..."@) and (@'...'@) without any success.我曾尝试使用双引号,双引号和单引号的组合,这里的字符串 (@"..."@) 和 (@'...'@) 没有任何成功。

Anyone have an idea on how to solve this?任何人都知道如何解决这个问题?

Thanks!谢谢! :: Petter :: 彼得

Yes, this is one of the Quoting issues documented in the Azure CLI docs:是的,这是 Azure CLI 文档中记录的引用问题之一:

When running Azure CLI commands in PowerShell, parsing errors will occur when the arguments contain special characters of PowerShell, such as at @ .在 PowerShell 中运行 Azure CLI 命令时,当参数包含 PowerShell 的特殊字符时,会发生解析错误,例如@ You can solve this problem by adding a backtick (`) before the special character to escape it, or by enclosing the argument with single or double quotes '/".您可以通过在特殊字符前添加反引号 (`) 来解决这个问题,或者用单引号或双引号 '/" 将参数括起来。

For example, az group deployment create --parameters @parameters.json doesn't work in PowerShell because @ is parsed as a splatting symbol .例如, az group deployment create --parameters @parameters.json在 PowerShell 中不起作用,因为@被解析为splatting 符号 To fix this, you may change the argument to `@parameters.json or '@parameters.json'.要解决此问题,您可以将参数更改为 `@parameters.json 或 '@parameters.json'。

Since what you have is a PS script already, the easiest way out would be to use the PowerShell equivalent of az vm run-command , which is Invoke-AzVMRunCommand .由于您已经拥有一个 PS 脚本,因此最简单的方法是使用az vm run-command的 PowerShell 等效项,即Invoke-AzVMRunCommand

Here is how you can use it:以下是如何使用它:

Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}

Other ways to run PowerShell scripts in your Windows VM by using Run Command are documented here . 此处介绍了使用 Run Command 在 Windows VM 中运行 PowerShell 脚本的其他方法。

But if you do have specific reasons to use Azure CLI commands within your PS script, you could also try using PowerShell's stop-parsing symbol --% in your command.但是,如果您确实有在 PS 脚本中使用 Azure CLI 命令的特定原因,您也可以尝试在您的命令中使用 PowerShell 的停止解析符号--% The stop-parsing symbol (--%), introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting input as PowerShell commands or expressions. PowerShell 3.0 中引入的停止解析符号 (--%) 指示 PowerShell 避免将输入解释为 PowerShell 命令或表达式。

So this should do the trick:所以这应该可以解决问题:

az --% vm run-command invoke --command-id RunPowerShellScript --resource-group '<myResourceGroup>' --name '<myVMName>' --scripts @script.ps1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM