-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctopusVariableSetExport.ps1
56 lines (42 loc) · 1.88 KB
/
OctopusVariableSetExport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Param(
[Parameter(Mandatory = $true, HelpMessage = 'Octopus URL')][string] $OctopusUrl,
[Parameter(Mandatory = $true, HelpMessage = 'User API Key')][string] $UserApiKey,
[Parameter(Mandatory = $true, HelpMessage = 'Variable Set Names')][string[]] $VariableSetNames
)
$excelFile = "$env:TEMP\Export.xlsx"
Write-Verbose -Verbose -Message "Save location: $excelFile"
Remove-Item $excelFile -ErrorAction Ignore
# Add Octopus.Client .NET library
$path = Join-Path (Get-Item ((Get-Package Octopus.Client).source)).Directory.FullName 'lib/net45/Octopus.Client.dll'
Add-Type -Path $path
function GetVariableSet {
Param (
[Parameter(Mandatory = $true)][string] $OctopusUrl,
[Parameter(Mandatory = $true)][string] $UserApiKey,
[Parameter(Mandatory = $true)][string] $VariableSetName
)
Process {
$export = @()
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint($OctopusUrl, $UserApiKey)
$repository = New-Object Octopus.Client.OctopusRepository($endpoint)
$library = $repository.LibraryVariableSets.FindByName($VariableSetName)
$variableset = $repository.VariableSets.Get($library.VariableSetId);
$variableset.Variables | ForEach-Object {
$environments = @()
$_.Scope.Values | ForEach-Object {
$environment = $repository.Environments.Get($_)
$environments += $environment.Name
}
$export += [PSCustomObject]@{
Name = $_.Name;
Value = $_.Value;
Scope = $environments -join ','
}
}
return $export
}
}
$VariableSetNames | ForEach-Object {
GetVariableSet -OctopusUrl $OctopusUrl -UserApiKey $UserApiKey -VariableSetName $_ |
Export-Excel $excelFile -WorksheetName $_ -AutoSize
}