Export/Import a Project fails with 'Machine' scoped variables error?

Hi Emil,

Thank you for getting in touch, and this is a great question. As you pointed out, machine-scopes on any variables included in what you’re attempting to export is a limitation of this feature, with no current way to ignore these.

How we’ve handled similar issues in the past is a script that removes the machine scopes from the source (storing the changes in a hashtable for easier reference when needing to re-add the scopes on the destination). However this is a modification of the source before the export, which you mention is not possible since the source is production-active. Also, since these are in global variable sets, you can’t simply clone the project as they would just be attached to the same static variable set, so approaching this situation will be tricky.

I’ve been attempting to come up with a workaround solution to export/import variable sets like you have mentioned in your other thread (we can continue this as one discussion in this thread to keep it in one place, if that’s okay!), though it’s proving to be taking time and effort to figure out, and I think this is similar to what it may end up looking like:

Export variable set:

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = ""
$octopusAPIKey = ""
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$ErrorActionPreference = "Stop";

$spaceName = ""
$varSetName = ""

# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

# Get project
$varSet = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/libraryvariablesets/all" -Headers $header) | Where-Object {$_.Name -eq $varSetName}

# Get project variables
#$projectVariables = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/libraryvariablesets/$($varSet.Id)" -Headers $header 
$projectVariables = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/variables/variableset-$($varSet.Id)" -Headers $header 
#$projectVariables.Variables

$varTable = @{}

foreach($var in $projectVariables.Variables) {

    $var.Scope.Machine
    if($var.Scope.Machine -ne $null){
      $varTable += @{$var.Id = @($var.Name, $var.Scope.Machine)}
      $var.Scope.PSObject.Properties.Remove("Machine")
    }

    #$var.Name
    #$var.Value
   
    #$var.isSensitive
}

$projectVariables | ConvertTo-Json -depth 10 | Out-File "path\to\variables.json"

Import variable set:

# Define octopus variables
$octopusURL = ""
$octopusAPIKey = ""
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

# Define working variables
$spaceName = ""
$variableSetFilePath = "path\to\variables.json"
$destinationVarSetName = ""

# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

# Get project
$varSet = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/libraryvariablesets/all" -Headers $header) | Where-Object {$_.Name -eq $destinationVarSetName}

# Get project variables from file
$projectVariablesFromFile = Get-Content -Raw -Path $variableSetFilePath | ConvertFrom-Json

write-host $projectVariablesFromFile

# Update the collection
Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/variables/variableset-$($varSet.Id)" -Headers $header -Body ($projectVariablesFromFile)

I hope this is helpful for the time being. I’m going to keep looking into this one shortly and will update with anything I find. :slight_smile:

Best regards,

Kenny