1

I have a PowerShell script in Azure DevOps Pipeline that outputs a list of possible IP address that Web App can use:

$deploymentOutputs=(ConvertFrom-Json '$(deploymentOutputs)')
$possibleOutboundIpAddresses=$($deploymentOutputs.possibleOutboundIpAddresses.value)
Write-Host $possibleOutboundIpAddresses

Output looks like this:

23.89.272.2,52.165.130.123,40.222.30.223

How can I transform the PowerShell script to output not comma separated values, but rather output those values as Each-Object?

23.89.272.2
52.165.130.123
40.222.30.223

Ideally, I'd love to also be able to add custom text before the values:

IP1 - 23.89.272.2
IP2 - 52.165.130.123
IP3 - 40.222.30.223

EDIT: The above examples were oversimplification of what I'm trying to do. What I really need to do is to create Azure DevOps variable for each entry in that output. I actually need to perform this PowerShell commands based on the number of IPs and values of IPs:

Write-Host "##vso[task.setvariable variable=$IP1;]23.89.272.2"
Write-Host "##vso[task.setvariable variable=$IP2;]52.165.130.123"
Write-Host "##vso[task.setvariable variable=$IP3;]40.222.30.223"
mklement0
  • 382,024
  • 64
  • 607
  • 775
WinBoss
  • 879
  • 1
  • 17
  • 40
  • As an aside: To avoid a distraction, can I suggest you remove the `Write-Host ` prefixes from your commands? – mklement0 Mar 13 '20 at 14:46
  • Write-Host is actually mandatory for creating Azure DevOps Variable from PowerShell. – WinBoss Mar 13 '20 at 14:51
  • 1
    For my own edification: Why? What happens if you write to the _success output stream_, which is where _data_ should go? `Write-Host` output is usually only captured by _outside callers_ that call PowerShell via its CLI, but so is success output. – mklement0 Mar 13 '20 at 14:54
  • 1
    See [this answer](https://stackoverflow.com/a/60534138/45375) for general information about `Write-Host` vs. `Write-Output` / _implicit_ output. In short: don't use `Write-Host` to output _data_ (return values). Removing the `WriteHost ` prefix _implicitly_ outputs the values. – mklement0 Mar 13 '20 at 14:57
  • Once I inspected the answer I realized that the rules are created by default. I was too quick to ask that question. I don't think any one would need it. – WinBoss Mar 19 '20 at 11:41

1 Answers1

5

You could use the String.Split() method:

$possibleOutboundIpAddresses.Split(",")

or the -split regex operator:

$possibleOutboundIpAddresses -split ','

If you want them as properties of an object, you could use ConvertFrom-Csv:

$ipAddressObject = $possibleOutboundIpAddresses |ConvertFrom-Csv -Header IP1,IP2,IP3

Although this might not be the best strategy if $possibleOutboundIpAddresses can have a variable number of values


For generating the Azure DevOps variable declarations you could do something like:

function ConvertTo-AzureVsoIPVariable {
    $c = 1
    $input -split ',' |ForEach-Object {
      '##vso[task.setvariable variable=$IP{0};]{1}' -f $c++, $_
    }
}

Then use like:

$possibleOutboundIpAddresses | ConvertTo-AzureVsoIPVariable
mklement0
  • 382,024
  • 64
  • 607
  • 775
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206