Azure DevOps管道循环,找不到路径Powershell脚本

sqserrrh  于 2023-08-05  发布在  Shell
关注(0)|答案(1)|浏览(110)

我有这个管道,它调用powershell脚本。powershell脚本需要为传入的每个值运行。所以我在参数中创建了一个名为VariableFiles的对象,并在步骤上创建了一个foreach。

pool: 
  vmImage: windows-latest

parameters:
  - name: variableFiles
    displayName: Variable File
    type: object
    default:
      - test
      - testApp
  - name: environment
    displayName: Environment
    type: string
    default: Dev
    values:
      - Dev
      - Test
      - Stag 
      - Prod

variables:
 - group: ADO_statefile 

name: 'ADO Automation'

stages:
- stage: powershellDeployment
  displayName: "PowerShell Deployment"
  jobs:
  - deployment: "deployment"
    displayName: "PowerShell Deployment"
    environment: 'ado - Dev'
    strategy:
      runOnce:
        deploy:
          steps:
            - ${{ each variableFile in parameters.variableFiles }}:          
              - template: steps.yml
                parameters:
                  spn: $(service_connection)
                  JsonPath: ${{ variableFile }}
                  validate: true

字符串
当我在variableFiles参数中有更多的1对象时,我得到这个错误:##[错误]无法将术语“D:\a\1\s\ADO\powershell\CreateAADGroup\createAADGroup.ps1”识别为cmdlet、函数、脚本文件或可执行程序的名称。
当我在variableFiles参数中只使用一个对象运行相同的脚本时,一切正常。
我真的不明白。
下面是模板的steps.yml,称为

steps:
  - checkout: self

  - task: PowerShell@2
    displayName: "List environment variables"
    inputs:
      pwsh: true
      targetType: "inline"
      script: |
        # List all pipeline variables
        dir env:

  - task: AzurePowerShell@5
    displayName: "create ADD groups"
    inputs:
        azureSubscription: ${{ parameters.spn }}
        pwsh: true
        azurePowerShellVersion: LatestVersion
        ScriptType: 'FilePath'
        ScriptPath: "$(Build.SourcesDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1"
        ScriptArguments: '-JsonPath ${{ parameters.JsonPath }}'


下面是powershell脚本:

Param(
    [string]$JsonPath
)

$parameters = Get-Content -Raw -Path "$($env:BUILD_SOURCESDIRECTORY)\ADO\terraform\$JsonPath.tfvars.json"  | ConvertFrom-Json
foreach ($GroupName in $parameters.AADGroupName) {
    # Check if the group already exists
    $existingGroup = Get-AzADGroup -DisplayName $GroupName -ErrorAction SilentlyContinue
    
    if ($existingGroup) {
        Write-Host "Group $GroupName already exists. Skipping creation."
    }
    else {
        Write-Host "Creating group: $GroupName"
        New-AzADGroup -DisplayName $GroupName -MailNickname $GroupName
    }
}


我已经尝试了这么多的事情,移动价值观和谷歌周围的一切主题。我迷路了。

x6492ojm

x6492ojm1#

你在用

$(Build.SourcesDirectory)

字符串
作为yaml管道中的ScriptPath,因此您将收到术语未识别错误。
Build.sourcesDirectory的预定义变量是c:\agent_work\1\s,您的源代码文件将在此下载到代理中。

$(System.DefaultWorkingDirectory)


System.DefaultWorkingDirectory将从存储库的根目录中正确获取powershell脚本和其他文件。
更新后的代码应该如下所示:

steps:
  - checkout: self

  - task: PowerShell@2
    displayName: "List environment variables"
    inputs:
      pwsh: true
      targetType: "inline"
      script: |
        # List all pipeline variables
        dir env:

  - ${{ each variableFile in parameters.variableFiles }}:
    - task: AzurePowerShell@5
      displayName: "create ADD groups"
      inputs:
        azureSubscription: ${{ parameters.spn }}
        pwsh: true
        azurePowerShellVersion: LatestVersion
        ScriptType: 'FilePath'
        ScriptPath: '$(System.DefaultWorkingDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1'
        ScriptArguments: '-JsonPath ${{ variableFile }}'


我尝试通过将Get-AzADGroup和New-AzADGroup命令存储在存储库中的脚本中来运行它们,并使用System.DefaultWorkingDirectory作为脚本文件的脚本路径,命令和管道成功运行,请参阅下面:-

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'devopsappsilicon'
    ScriptType: 'FilePath'
    ScriptPath: '$(System.DefaultWorkingDirectory)/azadgrp1.ps1'
    azurePowerShellVersion: 'LatestVersion'

输出:-

x1c 0d1x的数据


相关问题