Script variable creation

Hi,
Is there a way to script variable creation without using the octopus.client? I would like to write a powershell script that will parse my cscfg files and create the variables in octopus for me. Id like to do this with just restapi but i cant get the put to return anything other than 500

 # pull back var list
$varset = Invoke-RestMethod -Uri "$server/api/variables/variableset-$targetid" -Method Get -Headers $header
 # add var to list
foreach($var in $configvarlist.GetEnumerator())
{
 # build up var object    
$varobject = New-Object PSObject
$varobject | Add-Member -type NoteProperty -name id -value ''
$varobject | Add-Member -type NoteProperty -name name -value $var.name
$varobject | Add-Member -type NoteProperty -name Value -value $var.Value
$varobject | Add-Member -type NoteProperty -name Scope -value 'Environments-3'
$varobject | Add-Member -type NoteProperty -name IsSensitive -value 'False'
$varobject | Add-Member -type NoteProperty -name IsEditable -value 'True'
$varobject | Add-Member -type NoteProperty -name Prompt -value ''
 # add object to var list
$varset.Variables += @($varobject)
}
Invoke-RestMethod -Uri "$server/api/variables/variableset-$targetid" -Method Put -Headers $header -Body $varset

thanks
tim

Hi Tim,

Thanks for getting in touch!

Due to the way PowerShell objects work and the objects our API is expecting, I’ve modified your script slightly. I’ve tested this locally and it is working as expected.

 # pull back var list
$varset = Invoke-RestMethod -Uri "$server/api/variables/variableset-$targetid" -Method Get -Headers $header
 # add var to list
foreach($var in $configvarlist.GetEnumerator())
{
 # build up var object
$varobject = New-Object PSObject
$varobject | Add-Member -type NoteProperty -name Id -value ''
$varobject | Add-Member -type NoteProperty -name Name -value $var.name
$varobject | Add-Member -type NoteProperty -name Value -value $var.Value
# The scope object can be of type 'Environment', 'Machine', 'Role', 'Action'
# The value of the scope can also be multiple values, i.e. @('Environments-1', 'Environments-2', 'Environments-3')
$varobject | Add-Member -type NoteProperty -name Scope -value @{'Environment'= @('Environments-1')}
$varobject | Add-Member -type NoteProperty -name IsSensitive -value 'False'
$varobject | Add-Member -type NoteProperty -name IsEditable -value 'True'
# If you want the variable to be a Prompted variable, uncomment the below and fill in the proper values for the properties
#$varobject | Add-Member -type NoteProperty -name Prompt -value @{Label='Prompt Label';Description='Prompt Description';Required='False';}
 # add object to var list
$varset.Variables += @($varobject)
}

# We also need to convert the final object to JSON to be passed to our API 
# the default nested property depth for ConvertTo-Json is 3, 
# but our nested objects go deeper than that, so we need to specify the depth (5 seems to work fine
$body = $varset | ConvertTo-Json -Depth 5

Invoke-RestMethod -Uri "$server/api/variables/variableset-$targetid" -Method Put -Headers $header -Body $body

Hope that helps!

Henrik

that does it for me, THANKS!

What is $targetid here? Is this chunk of code part of a larger script?

Hi Bob,

That would be the ID of your variable set. See attached screenshot for visual reference.

I believe that’s the only info missing on that script.

Regards,
Dalmiro