I have read multiple questions regarding this but I can't make it work.
I want to pass a powershell object (hashtable) between tasks.
I set the hashtable here:
$output += @{
roleAssignments = $roleAssignments
subscriptionName = "$workloadName-$omgevingKeyLower"
subscriptionManagementGroupId = $workloadName
subscriptionTags = $tags
subscriptionWorkload = $subscriptionWorkload
virtualNetworkAddressSpace = $virtualNetworkAddressSpace
}
# Output the entire object as JSON
# the json part is optional as far as I'm concerned. Rather have plain hashtable
$outputJson = $output | ConvertTo-Json -Compress
Write-Output $output
I see the hashtable is returned at conclusion of the task that reads the file and sets the hashtable.
This is my pipeline code:
- task: PowerShell@2
displayName: 'Read YAML file'
name: ReadYaml
inputs:
targetType: filePath
filePath: ./scripts/readYaml.ps1
arguments: '-filePath ${{ parameters.yamlFilePath }}'
- task: AzureCLI@2
displayName: 'Create management group'
inputs:
azureSubscription: iaCS-ASE
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
echo "print the output"
& az account management-group create --name $(output.subscriptionManagementGroupId) --parent Landingzones
I want to do $(output.subscriptionName)
here. I've tried converting to and from json. accessing keys with and without []. I'm at a loss. Locally in PowerShell it works fine. But ado throws a wrench in the works.
I have read multiple questions regarding this but I can't make it work.
I want to pass a powershell object (hashtable) between tasks.
I set the hashtable here:
$output += @{
roleAssignments = $roleAssignments
subscriptionName = "$workloadName-$omgevingKeyLower"
subscriptionManagementGroupId = $workloadName
subscriptionTags = $tags
subscriptionWorkload = $subscriptionWorkload
virtualNetworkAddressSpace = $virtualNetworkAddressSpace
}
# Output the entire object as JSON
# the json part is optional as far as I'm concerned. Rather have plain hashtable
$outputJson = $output | ConvertTo-Json -Compress
Write-Output $output
I see the hashtable is returned at conclusion of the task that reads the file and sets the hashtable.
This is my pipeline code:
- task: PowerShell@2
displayName: 'Read YAML file'
name: ReadYaml
inputs:
targetType: filePath
filePath: ./scripts/readYaml.ps1
arguments: '-filePath ${{ parameters.yamlFilePath }}'
- task: AzureCLI@2
displayName: 'Create management group'
inputs:
azureSubscription: iaCS-ASE
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
echo "print the output"
& az account management-group create --name $(output.subscriptionManagementGroupId) --parent Landingzones
I want to do $(output.subscriptionName)
here. I've tried converting to and from json. accessing keys with and without []. I'm at a loss. Locally in PowerShell it works fine. But ado throws a wrench in the works.
- I don't think this is the correct duplicate, but I believe setting pipeline variables is what you're after. This will allow all following tasks to access a variable set in an ADO task – C.Nivs Commented Mar 4 at 14:42
- I don't think this applies because I do use two tasks? Also I've tried it with set-output but it doesn't work either and I've read in another answer that this command converts the variable into a string. I've seen this happen that the first line of the hash gets taken as the value for the set-output var when accessing it – MyName Commented Mar 4 at 14:53
- I agree with @C.Nivs, output variables is probably what you're looking for. Take a look at the documentation link I posted to understand how to use them. – Rui Jarimba Commented Mar 4 at 15:01
- Each task executes as a separate process and then terminates. If you want to carry state forward, use output variables. – bryanbcook Commented Mar 4 at 17:57
3 Answers
Reset to default 3The variables generated in one PowerShell session cannot be referenced in another session. This is true even for local PowerShell prompt not only specific to the task of Azure Pipelines.
You need to set an output variable for use in the same job and according to the document, All variables are strings.
Based on the possible data shapes you mentioned so far, here is a sample script and YAML definition to help understand how to safely output a one-line string across tasks in the same agent job.
scripts/readYaml.ps1
$roleAssignments = @(
@{
'principalName' = "[email protected]"
'roleDefinitionName' = "User Access Administrator"
'scope' = "/"
}
@{
'principalName' = "[email protected]"
'roleDefinitionName' = "Owner"
'scope' = "/providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000"
}
)
$workloadName = "sample workload"
$omgevingKeyLower = "lowerkey"
$subscriptionWorkload = "some string"
$tags = @{
'Workload' = "workloadTagValue"
'TestTag' = "testTagValue"
}
$virtualNetworkAddressSpace = @{
addressPrefixes = @(
"10.0.0.0/16"
"192.168.0.0/24"
)
}
$output += @{
roleAssignments = $roleAssignments
subscriptionName = "$workloadName-$omgevingKeyLower"
subscriptionManagementGroupId = $workloadName
subscriptionTags = $tags
subscriptionWorkload = $subscriptionWorkload
virtualNetworkAddressSpace = $virtualNetworkAddressSpace
}
$output | ConvertTo-Json -Depth 10
$outputJson = $output | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=outputJson]$outputJson"
trigger: none
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell@2
displayName: 'Read YAML file'
name: ReadYaml
inputs:
targetType: filePath
filePath: ./scripts/readYaml.ps1
# arguments: '-filePath ${{ parameters.yamlFilePath }}'
- pwsh: |
# Using single quotes to safely handle JSON strings
Write-Host outputJson is '$(outputJson)'
$output = '$(outputJson)' | ConvertFrom-Json
Write-Host "================ Check output from upstream step in the same job ================"
Write-Host $output.roleAssignments[0].principalName
$output | ConvertTo-Json -Depth 10
I think this might be an X-Y Problem. You are focusing on the way you access the variable in your file, when really you just need the variable set in the pipeline. So set your variables into the pipeline directly, don't rely on the json encoding and accessing them as json:
- pwsh: |
Write-Host "##vso[task.setvariable variable=subscriptionManagementGroupId;]$workloadName"
Write-Host "##vso[task.setvariable variable=subscriptionName;]$workloadName-$omgevingKeyLower"
- task: AzureCLI@2
displayName: 'Create management group'
inputs:
azureSubscription: iaCS-ASE
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
echo "print the output"
# notice here that I've dropped the `output.` notation
& az account management-group create --name $(subscriptionManagementGroupId) --parent Landingzones
You may use logging commands: SetVariable: Initialize or modify the value of a variable. Then read as environment variable. From the example:
Set the variables:
- pwsh: |
Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
name: SetVars
Read the variables:
- pwsh: |
Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745038018a4607639.html
评论列表(0条)