Hi,
High-level description:
Copy variables from project level to library set (copy only unmatched)
I can GET project-level variables for the project ‘KT’ using REST API calls below. And also able to GET variable set ID using below REST API call.
And created an object to capture project-level variable output to $results(), where I’m getting the result correctly. But while executing the “PUT” command to copy variables from project level to library set I’m getting 405 methods not allowed error.
Get project
$libSet = (Invoke-RestMethod -Method Get -Uri ($AppOctopusSrvPRODURI + “/api/projects/all”) -ContentType application/json -Headers $oBaseURIHdr) | Where-Object {$_.Name -eq ‘KT’}
Get project variables
$libSetDet = Invoke-RestMethod -Method Get -Uri ($AppOctopusSrvPRODURI + $project.links.Variables) -ContentType application/json -Headers $oBaseURIHdr
- get library set details
$librarySet = (Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + “/api/libraryvariablesets/?partialName=$librarySetName”) -ContentType application/json -Headers $oBaseURIHdr -Method Get).items
$librarySetDet = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + $librarySet.links.Variables) -ContentType application/json -Headers $oBaseURIHdr -Method Get
Here is the full script:
The script logic is working fine except “PUT” REST API call failure. Not sure what is missing. Please do the needful.
- functions
function DO-BuildList () {
param (
[Parameter(Mandatory=$true)][System.Object] $Userlist
)
[system.Object]$returnList = @()
foreach ($item in $userlist) {
$itemName = ($item.Split(’,’)).Trim()
$returnList = $returnList + $itemName
}
return $returnList
}
- input vars
$ProjectName = ‘KT’ # Ocotpus library set name
$librarySetName = ‘Sample.Test.Appway’
$outRoot = ‘C:\Users<user>\Desktop\Output’ # output root path - creates if it doesn’t exist
$outFldr = ‘LibSetExport’ # output folder name
- general vars
[System.Collections.ArrayList]$results = @()
- Octopus vars
$AppOctopusSrvPRODURI = ‘’
$AppOctopusSrvPRODAPIKey = ‘removed for security’
[Hashtable]$oBaseURIHdr = @{ “X-Octopus-ApiKey” = $AppOctopusSrvPRODAPIKey }
BEGIN
- get environments
$envs = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + “/api/environments/all”) -ContentType application/json -Headers $oBaseURIHdr -Method Get
Get projec
$libSet = (Invoke-RestMethod -Method Get -Uri ($AppOctopusSrvPRODURI + “/api/projects/all”) -ContentType application/json -Headers $oBaseURIHdr) | Where-Object {$_.Name -eq $ProjectName}
Get project variables
$libSetDet = Invoke-RestMethod -Method Get -Uri ($AppOctopusSrvPRODURI + $libSet.links.Variables) -ContentType application/json -Headers $oBaseURIHdr
- get library set details
$librarySet = (Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + “/api/libraryvariablesets/?partialName=$librarySetName”) -ContentType application/json -Headers $oBaseURIHdr -Method Get).items
$librarySetDet = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + $librarySet.links.Variables) -ContentType application/json -Headers $oBaseURIHdr -Method Get
if ($results.Count -gt 0) { $results.Clear() }
foreach ($var in $libSetDet.Variables) {
$scopeString = $null
$varobj = $null
# - handle scoped vs non-scoped variables differently
if ( $var.scope.psObject.Properties.Name -eq 'Environment' ) {
# - build scope string
$scopeList = DO-BuildList -Userlist $var.Scope.Environment
foreach ($item in $scopeList ) {
$scopeName = ($envs | ? {$_.Id -eq $item}).name + ', '
$scopeString = $scopeString + $scopeName
}
}
elseif ( $var.scope.psObject.Properties.Name -eq 'Role' ) {
$scopeList = DO-BuildList -Userlist $var.Scope.Role
foreach ($item in $scopeList ) {
$scopeName = $item + ', '
$scopeString = $scopeString + $scopeName
}
}
else { $scopeString = 'NONE'}
$scopeString = $scopeString.TrimEnd(', ')
# - create custom obj
$varobj = [pscustomobject] @{
Source = $libSetName
Name = $var.Name
Value = $var.Value
Scope = $scopeString
IsSensitive = $var.IsSensitive
}
# - add to final results obj
$results.Add($varobj) | Out-Null
}
- export results to csv
$now = get-date -Format “yyMMdd_HHmm”
$outFile = $libSetName
$outPath = ($outRoot + ‘’ + $outFldr + ‘’ + $outFile + ‘_’ + $now +’.csv’)
- create target folder
if ( ! ( test-path ($outRoot + ‘’ + $outFldr))) { New-Item -Path ($outRoot + ‘’ + $outFldr) -ItemType Directory -Force | Out-Null }
- output file
if ( test-path $outPath ) { Remove-Item $outPath -Force | Out-Null }
$results | export-csv $outPath -NoTypeInformation -Force
Update the collection
#Invoke-RestMethod -Method Put -Uri “$octopusURL/api/variables/$libSetDet” -Headers $header -Body ($projectVariables | ConvertTo-Json -Depth 10)
$UpdatedLibraryVariableSet = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + “/api/libraryvariablesets/?partialName=$librarySetName”) -Headers $oBaseURIHdr -Method Put -Body ($results | ConvertTo-Json -Depth 10)
#$envs = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + “/api/environments/all”) -ContentType application/json -Headers $oBaseURIHdr -Method Get
Error stack trace:
Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed.
At line:99 char:30
- … riableSet = Invoke-RestMethod -Uri ($AppOctopusSrvPRODURI + "/api/lib …
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand