Accessing Collection Variables in PowerShell

So I have defined a couple of collection variables, something along the lines of;

RSDataSources[Report1].SecurityGroup
RSDataSource[Report1].Roles
RSDataSources[Report2].SecurityGroup
RSDataSource[Report2].Roles

I’ve used a PowerShell script task.

How do access $RSDataSources in powershell and iterate through the different reports (in this case Report1, Report2).

I would expect RSDataSources would have to be some hashtable ?

I’ve seen examples in Variable Substitution where you can iterate through Variable collections but nothing about how to access the Variable collections in PowerShell.

Hi Nick,

Thanks for getting in touch, that’s a great question.

Variables are provided to PowerShell scripts as a dictionary where the key is the variable name and value is the variable value. Collection variables are not treated specially, so you’ll need to provide the full variable name in order to get a value back.

Iterating reports would look like:

$reports = "Report1", "Report2"
$reports | % {
	Write-Host $OctopusParameters["RSDataSource[$_].Roles"]
}

If you don’t know the report names in advance you may be able to construct them by querying the keys of the variable dictionary. Something like this should get you started:

$OctopusParameters.Keys | ? { $_ -Like "RSDataSource*" } | % { Write-Host $_ }

Look forward to hearing if this helps.

Cheers,
Shane

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.