Skip to content

Commit 2e8a1fb

Browse files
authored
Fix Add -PassThru Parameter to Export Cmdlets #79 (#92)
* Fix Add -PassThru Parameter to Export Cmdlets #79
1 parent bc6173a commit 2e8a1fb

21 files changed

Lines changed: 588 additions & 75 deletions

src/PSRule.Rules.AzureDevOps/Functions/Common.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ function Get-AzDevOpsProject {
157157
.DESCRIPTION
158158
Export the Azure DevOps Project using Azure DevOps Rest API to a JSON file
159159
160+
.PARAMETER Project
161+
Project name for Azure DevOps
162+
163+
.PARAMETER OutputPath
164+
Output path for JSON files
165+
166+
.PARAMETER PassThru
167+
Return the exported project as objects to the pipeline instead of writing to a file
168+
160169
.EXAMPLE
161170
Export-AzDevOpsProject -Project $Project -OutputPath $OutputPath
162171
@@ -170,9 +179,12 @@ function Export-AzDevOpsProject {
170179
[Parameter(Mandatory=$true)]
171180
[string]
172181
$Project,
173-
[Parameter(Mandatory=$true)]
182+
[Parameter(ParameterSetName = 'JsonFile')]
174183
[string]
175-
$OutputPath
184+
$OutputPath,
185+
[Parameter(ParameterSetName = 'PassThru')]
186+
[switch]
187+
$PassThru
176188
)
177189
if ($null -eq $script:connection) {
178190
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -188,6 +200,11 @@ function Export-AzDevOpsProject {
188200
catch {
189201
throw "Failed to get project $Project from Azure DevOps"
190202
}
191-
$response | ConvertTo-Json | Out-File -FilePath "$OutputPath/$Project.prj.ado.json"
203+
if($PassThru) {
204+
Write-Output $response
205+
} else {
206+
Write-Verbose "Exporting project $Project as file $Project.prj.ado.json"
207+
$response | ConvertTo-Json | Out-File -FilePath "$OutputPath/$Project.prj.ado.json"
208+
}
192209
}
193210
# End of Function Export-AzDevOpsProject

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Groups.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ Export-ModuleMember -Function Get-AzDevOpsGroupDetails
122122
.PARAMETER OutputPath
123123
The folder path to the JSON file to export to
124124
125+
.PARAMETER PassThru
126+
Return the group details as an object instead of exporting to a file
127+
125128
.EXAMPLE
126129
Export-AzDevOpsGroups -Project 'MyProject' -OutputPath 'C:\Temp\'
127130
@@ -134,9 +137,12 @@ Function Export-AzDevOpsGroups {
134137
[Parameter(Mandatory=$true)]
135138
[string]
136139
$Project,
137-
[Parameter(Mandatory=$true)]
140+
[Parameter(ParameterSetName='JsonFile')]
138141
[string]
139-
$OutputPath
142+
$OutputPath,
143+
[Parameter(ParameterSetName='PassThru')]
144+
[switch]
145+
$PassThru
140146
)
141147
if($null -eq $script:connection) {
142148
throw 'Not connected to Azure DevOps. Run Connect-AzDevOps first.'
@@ -157,7 +163,11 @@ Function Export-AzDevOpsGroups {
157163

158164
$groupDetails += $thisGroup
159165
}
160-
$groupDetails | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\groups.ado.json"
166+
if($PassThru) {
167+
Write-Output $groupDetails
168+
} else {
169+
$groupDetails | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\groups.ado.json"
170+
}
161171
}
162172
Export-ModuleMember -Function Export-AzDevOpsGroups
163173
# End of Function Export-AzDevOpsGroups

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Pipelines.Core.ps1

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Export-ModuleMember -Function Get-AzDevOpsPipelineYaml
203203
.PARAMETER OutputPath
204204
Output path for YAML file
205205
206+
.PARAMETER PassThru
207+
Pass the YAML definition to the pipeline object
208+
206209
.EXAMPLE
207210
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $PipelineId -OutputPath $OutputPath
208211
#>
@@ -218,9 +221,12 @@ function Export-AzDevOpsPipelineYaml {
218221
[Parameter(Mandatory)]
219222
[string]
220223
$PipelineName,
221-
[Parameter(Mandatory)]
224+
[Parameter(ParameterSetName = 'YamlFile')]
222225
[string]
223-
$OutputPath
226+
$OutputPath,
227+
[Parameter(ParameterSetName = 'PassThru')]
228+
[switch]
229+
$PassThru
224230
)
225231
if ($null -eq $script:connection) {
226232
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -236,8 +242,12 @@ function Export-AzDevOpsPipelineYaml {
236242
return $null
237243
} else {
238244
$yaml += $yamlTemp}
239-
Write-Verbose "Exporting YAML definition to $OutputPath\$PipelineName.yaml"
240-
$yaml | Out-File "$OutputPath\$PipelineName.yaml"
245+
if ($PassThru) {
246+
Write-Output $yaml
247+
} else {
248+
Write-Verbose "Exporting YAML definition to $OutputPath\$PipelineName.yaml"
249+
$yaml | Out-File "$OutputPath\$PipelineName.yaml"
250+
}
241251
}
242252
Export-ModuleMember -Function Export-AzDevOpsPipelineYaml
243253
# End of Function Export-AzDevOpsPipelineYaml
@@ -256,6 +266,9 @@ Export-ModuleMember -Function Export-AzDevOpsPipelineYaml
256266
.PARAMETER OutputPath
257267
Output path for JSON files
258268
269+
.PARAMETER PassThru
270+
Pass the pipeline object to the pipeline object instead of exporting to a file
271+
259272
.EXAMPLE
260273
Export-AzDevOpsPipelines -Project $Project -OutputPath $OutputPath
261274
#>
@@ -265,9 +278,12 @@ function Export-AzDevOpsPipelines {
265278
[Parameter(Mandatory)]
266279
[string]
267280
$Project,
268-
[Parameter(Mandatory)]
281+
[Parameter(ParameterSetName = 'JsonFile')]
269282
[string]
270-
$OutputPath
283+
$OutputPath,
284+
[Parameter(ParameterSetName = 'PassThru')]
285+
[switch]
286+
$PassThru
271287
)
272288
if ($null -eq $script:connection) {
273289
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -290,16 +306,24 @@ function Export-AzDevOpsPipelines {
290306
} else {
291307
Write-Verbose "Token Type is set to ReadOnly, no pipeline ACLs will be returned"
292308
}
293-
Write-Verbose "Exporting pipeline $($pipeline.name) to JSON file"
294-
Write-Verbose "Exporting pipeline as JSON file to $OutputPath\$($pipeline.name).ado.pl.json"
295-
$pipeline | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$($pipeline.name).ado.pl.json"
296-
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
297-
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
298-
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"
299-
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -OutputPath $OutputPath
300-
309+
If ($PassThru) {
310+
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
311+
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
312+
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"
313+
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -PassThru
314+
}
315+
Write-Output $pipeline
316+
} else {
317+
Write-Verbose "Exporting pipeline $($pipeline.name) to JSON file"
318+
Write-Verbose "Exporting pipeline as JSON file to $OutputPath\$($pipeline.name).ado.pl.json"
319+
$pipeline | ConvertTo-Json -Depth 100 | Out-File "$OutputPath\$($pipeline.name).ado.pl.json"
320+
if ($pipeline.configuration.type -eq 'yaml' -and $pipeline.configuration.repository.type -eq 'azureReposGit') {
321+
Write-Verbose "Pipeline $($pipeline.name) is a YAML pipeline"
322+
Write-Verbose "Getting YAML definition for pipeline $($pipeline.name)"
323+
324+
Export-AzDevOpsPipelineYaml -Project $Project -PipelineId $pipeline.id -PipelineName $pipeline.name -OutputPath $OutputPath
325+
}
301326
}
302-
303327
}
304328
}
305329
Export-ModuleMember -Function Export-AzDevOpsPipelines

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Pipelines.Environments.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ Export-ModuleMember -Function Get-AzDevOpsEnvironmentChecks
126126
.PARAMETER Project
127127
Project name for Azure DevOps
128128
129+
.PARAMETER OutputPath
130+
Path to output JSON files
131+
132+
.PARAMETER PassThru
133+
Return the exported environments as objects to the pipeline instead of writing to a file
134+
129135
.EXAMPLE
130136
Export-AzDevOpsEnvironmentChecks -Project $Project
131137
#>
@@ -136,9 +142,12 @@ function Export-AzDevOpsEnvironmentChecks {
136142
[Parameter(Mandatory)]
137143
[string]
138144
$Project,
139-
[Parameter(Mandatory)]
145+
[Parameter(ParameterSetName = 'JsonFile')]
140146
[string]
141-
$OutputPath
147+
$OutputPath,
148+
[Parameter(ParameterSetName = 'PassThru')]
149+
[switch]
150+
$PassThru
142151

143152
)
144153
if ($null -eq $script:connection) {
@@ -159,9 +168,13 @@ function Export-AzDevOpsEnvironmentChecks {
159168
$environment | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.{2}" -f $script:connection.Organization,$Project,$environment.name)
160169
$checks = @(Get-AzDevOpsEnvironmentChecks -Project $Project -Environment $environment.id)
161170
$environment | Add-Member -MemberType NoteProperty -Name checks -Value $checks
162-
Write-Verbose "Exporting environment $($environment.name) to JSON"
163-
Write-Verbose "Output file: $OutputPath\$($environment.name).ado.env.json"
164-
$environment | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($environment.name).ado.env.json"
171+
if($PassThru) {
172+
Write-Output $environment
173+
} else {
174+
Write-Verbose "Exporting environment $($environment.name) to JSON"
175+
Write-Verbose "Output file: $OutputPath\$($environment.name).ado.env.json"
176+
$environment | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($environment.name).ado.env.json"
177+
}
165178
}
166179
}
167180
}

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Pipelines.Releases.ps1

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ Export-ModuleMember -Function Get-AzDevOpsReleaseDefinitionAcls
122122
.PARAMETER OutputPath
123123
The path to the directory where the JSON files will be exported.
124124
125+
.PARAMETER PassThru
126+
If set, the function will return the release definitions as objects instead of writing them to a file.
127+
125128
.EXAMPLE
126129
Export-AzDevOpsReleaseDefinitions -Project 'myproject' -OutputPath 'C:\temp'
127130
#>
@@ -131,8 +134,11 @@ Function Export-AzDevOpsReleaseDefinitions {
131134
[Parameter(Mandatory)]
132135
[string]$Project,
133136

134-
[Parameter(Mandatory)]
135-
[string]$OutputPath
137+
[Parameter(ParameterSetName = 'JsonFile')]
138+
[string]$OutputPath,
139+
140+
[Parameter(ParameterSetName = 'PassThru')]
141+
[switch]$PassThru
136142
)
137143
if ($null -eq $script:connection) {
138144
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -155,8 +161,6 @@ Function Export-AzDevOpsReleaseDefinitions {
155161
throw $_.Exception.Message
156162
}
157163
$definitionName = $response.name
158-
Write-Verbose "Exporting release definition $definitionName as file $definitionName.ado.rd.json"
159-
$definitionPath = Join-Path -Path $OutputPath -ChildPath "$definitionName.ado.rd.json"
160164
# Add an ObjectType of Azure.DevOps.Pipelines.Releases.Definition to the response
161165
$response | Add-Member -MemberType NoteProperty -Name 'ObjectType' -Value 'Azure.DevOps.Pipelines.Releases.Definition'
162166
$response | Add-Member -MemberType NoteProperty -Name 'ObjectName' -Value ("{0}.{1}.{2}" -f $script:connection.Organization,$Project,$definitionName)
@@ -178,7 +182,14 @@ Function Export-AzDevOpsReleaseDefinitions {
178182
} else {
179183
Write-Warning "The ReadOnly token type is not supported for ACL export"
180184
}
181-
$response | ConvertTo-Json -Depth 100 | Out-File -FilePath $definitionPath
185+
# If the PassThru switch is set, return the response object
186+
if ($PassThru) {
187+
Write-Output $response
188+
} else {
189+
Write-Verbose "Exporting release definition $definitionName as file $definitionName.ado.rd.json"
190+
$definitionPath = Join-Path -Path $OutputPath -ChildPath "$definitionName.ado.rd.json"
191+
$response | ConvertTo-Json -Depth 100 | Out-File -FilePath $definitionPath
192+
}
182193
}
183194
}
184195
}

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Pipelines.Settings.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Export-ModuleMember -Function Get-AzDevOpsPipelinesSettings
5353
.PARAMETER OutputPath
5454
Output path for JSON files
5555
56+
.PARAMETER PassThru
57+
Return the exported pipelines settings as objects to the pipeline instead of writing to a file
58+
5659
.EXAMPLE
5760
Export-AzDevOpsPipelinesSettings -Project $Project -OutputPath $OutputPath
5861
#>
@@ -62,9 +65,12 @@ function Export-AzDevOpsPipelinesSettings {
6265
[Parameter(Mandatory)]
6366
[string]
6467
$Project,
65-
[Parameter(Mandatory)]
68+
[Parameter(ParameterSetName = 'JsonFile')]
6669
[string]
67-
$OutputPath
70+
$OutputPath,
71+
[Parameter(ParameterSetName = 'PassThru')]
72+
[switch]
73+
$PassThru
6874
)
6975
if ($null -eq $script:connection) {
7076
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -74,7 +80,11 @@ function Export-AzDevOpsPipelinesSettings {
7480
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name ObjectType -Value 'Azure.DevOps.Pipelines.Settings'
7581
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name ObjectName -Value ("{0}.{1}.PipelineSettings" -f $script:connection.Organization,$Project)
7682
$pipelinesSettings | Add-Member -MemberType NoteProperty -Name Name -Value $Project
77-
$pipelinesSettings | ConvertTo-Json -Depth 10 | Out-File (Join-Path -Path $OutputPath -ChildPath "$Project.ado.pls.json")
83+
if ($PassThru) {
84+
Write-Output $pipelinesSettings
85+
} else {
86+
$pipelinesSettings | ConvertTo-Json -Depth 10 | Out-File (Join-Path -Path $OutputPath -ChildPath "$Project.ado.pls.json")
87+
}
7888
}
7989
Export-ModuleMember -Function Export-AzDevOpsPipelinesSettings
8090
# End of Function Export-AzDevOpsPipelinesSettings

src/PSRule.Rules.AzureDevOps/Functions/DevOps.Repos.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ Export-ModuleMember -Function Get-AzDevOpsRepositoryGhas
392392
.PARAMETER OutputPath
393393
Output path for JSON file
394394
395+
.PARAMETER PassThru
396+
Return the exported repos as objects to the pipeline instead of writing to a file
397+
395398
.EXAMPLE
396399
Export-AzDevOpsReposAndBranchPolicies -Project $Project -OutputPath $OutputPath
397400
@@ -404,9 +407,12 @@ function Export-AzDevOpsReposAndBranchPolicies {
404407
[Parameter(Mandatory)]
405408
[string]
406409
$Project,
407-
[Parameter(Mandatory)]
410+
[Parameter(ParameterSetName = 'JsonFile')]
408411
[string]
409-
$OutputPath
412+
$OutputPath,
413+
[Parameter(ParameterSetName = 'PassThru')]
414+
[switch]
415+
$PassThru
410416
)
411417
if ($null -eq $script:connection) {
412418
throw "Not connected to Azure DevOps. Run Connect-AzDevOps first"
@@ -471,11 +477,15 @@ function Export-AzDevOpsReposAndBranchPolicies {
471477
$repoAcls = Get-AzDevOpsRepositoryAcls -ProjectId $repo.project.id -RepositoryId $repo.id
472478
$repo | Add-Member -MemberType NoteProperty -Name Acls -Value $repoAcls
473479
}
474-
475-
# Export repo object to JSON file
476-
Write-Verbose "Exporting repo $($repo.name) and its branches to JSON as file $($repo.name).ado.repo.json"
477480
$branches += $repo
478-
$branches | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($repo.name).ado.repo.json"
481+
# If the PassThru switch is set, return the repo object
482+
if ($PassThru) {
483+
Write-Output $branches
484+
} else {
485+
# Export repo object to JSON file
486+
Write-Verbose "Exporting repo $($repo.name) and its branches to JSON as file $($repo.name).ado.repo.json"
487+
$branches | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($repo.name).ado.repo.json"
488+
}
479489
}
480490
}
481491
}

src/PSRule.Rules.AzureDevOps/Functions/DevOps.RetentionSettings.ps1

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Export-ModuleMember -Function Get-AzDevOpsRetentionSettings
6262
.PARAMETER OutputPath
6363
The path to export the retention settings to.
6464
65+
.PARAMETER PassThru
66+
If set, the function will return the retention settings as objects instead of writing them to a file.
67+
6568
.EXAMPLE
6669
Get-AzDevOpsRetentionSettings -Project 'MyProject' -OutputPath 'C:\Temp\'
6770
@@ -75,11 +78,19 @@ Function Export-AzDevOpsRetentionSettings {
7578
[string]
7679
$Project,
7780

78-
[Parameter(Mandatory=$true)]
81+
[Parameter(ParameterSetName = 'JsonFile')]
7982
[string]
80-
$OutputPath
83+
$OutputPath,
84+
85+
[Parameter(ParameterSetName = 'PassThru')]
86+
[switch]
87+
$PassThru
8188
)
8289
$settings = Get-AzDevOpsRetentionSettings -Project $Project
83-
$settings | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($Project).ret.ado.json"
90+
if($PassThru) {
91+
Write-Output $settings
92+
} else {
93+
$settings | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\$($Project).ret.ado.json"
94+
}
8495
}
8596
Export-ModuleMember -Function Export-AzDevOpsRetentionSettings

0 commit comments

Comments
 (0)