【问题标题】:How to add argument values with underscore in Azure DevOps PowerShell Pipeline task如何在 Azure DevOps PowerShell Pipeline 任务中添加带下划线的参数值
【发布时间】:2020-09-08 16:33:19
【问题描述】:

我有一个带有多个参数的 Azure DevOps 管道来创建 VM

    
task: AzurePowerShell@5
  inputs:
    azureSubscription: 'Visual Studio Enterprise Subscription – MPN(07f41212-2053-434e-XXXX)'
    ScriptType: 'FilePath'
    ScriptPath: '$(System.DefaultWorkingDirectory)/CreateNewVMforImage.ps1'
    ScriptArguments: -azSubscriptionId "07f41212-2053-434e-XXXXX" -azResourceGroupName "AZREUSMGMTRG" -azSnapshotName "SNAPSHOT2GOOD" -azNewOSDiskName "ImageOSDisk5"  -azVMLocation "eastus" -azVMNetwork "MGMTVNET" -azVMName "AZREUSLSIMG6" 
    
    azurePowerShellVersion: LatestVersion
    pwsh: true

如果我添加以下两个参数值,则管道执行错误输出,并且我理解这是由于下划线而发生的,因为其他值中没有任何下划线并且没有问题。只有当值在下面有下划线时,它才会在管道执行时给出错误。

    -azVMStorageType "Standard_LRS"
    -azVMSize "Standard_B2ms"

这是错误文本。

A positional parameter cannot be found that accepts an argument

我的问题是如何在管道 YAML 文件中添加带有下划线的参数值?

PS参数如下

[CmdletBinding()]
param (
    
#Subscription ID
    [Parameter(Mandatory=$true)]
    [string]
    $azSubscriptionId,
    
    #ResourceGroupName
    [Parameter(Mandatory=$true)]
    [string]
    $azResourceGroupName,

    #MotherSnapshotName
    [Parameter(Mandatory=$true)]
    [string]
    $azSnapshotName,

    #NewVMOSDiskName
    [Parameter(Mandatory=$true)]
    [string]
    $azNewOSDiskName,

    #VM Storage Type 
    [Parameter(Mandatory=$true)]
    [string]
    $azVMStorageType,

    #VM Location
    [Parameter(Mandatory=$true)]
    [string]
    $azVMLocation,

    #VM Network
    [Parameter(Mandatory=$true)]
    [string]
    $azVMNetwork,

    #VM Name
    [Parameter(Mandatory=$true)]
    [string]
    $azVMName,

    #VM Size
    [Parameter(Mandatory=$true)]
    [string]
    $azVMSize

)

任何帮助将不胜感激。

【问题讨论】:

  • 提供错误文本会很有用。
  • 嗨@askwizard,我可以成功地在参数中使用_。能否请您删除下划线并再试一次,然后在这里分享结果?此外,请将变量 system.debug 设置为 true,然后在此处分享完整的详细日志。谢谢。
  • 没有这两个值的参数可以创建VM,只有当我在yaml文件中添加这两个参数值时才会出现问题。
  • 嗨@askwizard,您使用的是哪个代理,托管代理还是自托管代理?您能否与我们分享完整的 .ps1 文件和详细日志?我需要检查日志并帮助你。谢谢。

标签: powershell azure-devops yaml azure-pipelines


【解决方案1】:

根据doc:用户自定义变量可以由字母、数字、.、_字符组成。 我可以在管道 YAML 文件中成功使用 add 参数值和下划线。 示例:

powershell.ps1

[CmdletBinding()]
param (
    
    #Subscription ID
    [Parameter(Mandatory=$true)]
    [string]
    $test01,
    
    #ResourceGroupName
    [Parameter(Mandatory=$true)]
    [string]
    $test02,

    #MotherSnapshotName
    [Parameter(Mandatory=$true)]
    [string]
    $test03

)

 Write-Host "test01 is" $test01
 Write-Host "test02 is" $test02
 Write-Host "test03 is" $test03

YAML 构建:

steps:
- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'Microsoft Azure Internal Consumption (daaeef3e-d7fe-49e8-baaa-b3df9d072825)'
    ScriptType: 'FilePath'
    ScriptPath: 'powershell.ps1'
    ScriptArguments: '-test01 "test" -test02 "Just_a_test" -test03 "Just a test"'
    azurePowerShellVersion: 'LatestVersion'

结果:

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2020-06-11
    • 2020-07-07
    • 1970-01-01
    • 2020-10-07
    • 2020-02-02
    • 1970-01-01
    • 2020-09-28
    • 2021-07-30
    相关资源
    最近更新 更多