0

Trying to append the index value of a for loop to the end of a string, but I'm having some trouble getting the desired result.

Here's a short code block of what I'm working with:

EDIT:

$apiKey = "ZZZ-ZZZZZ"
$octoHeader = @{ "X-Octopus-ApiKey" = $apiKey }

# GET Source deployment process from Deployment Scaffold project
$json = irm "http://octopusserver/api/deploymentprocesses/deploymentprocess-projects-123" -Headers $octoHeader

$DeploymentSteps = $json.Steps

# Hash table to hold steps
$steps = @{}

function Get-StepType
(
  $StepType
)
{
  foreach ( $Step in $DeploymentSteps | where { $_.Name -eq $StepType } )
  {
    return $Step
  }
}

function Copy-Steps
(
  $StepType,
  $StepCount
)
{
  $Step = Get-StepType -StepType $StepType 

  1..$StepCount | % {

    $Step.Id = ''
    $Step.Name += $_

    # Add step to hash
    $steps.Add("step$($steps.Count + 1)", $step) 
  }  
}

Copy-Steps -StepType 'Service' -StepCount 2 

$steps

And this is the result:

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
step1                          @{Id=; Name=Service12; Actions=S...
step2                          @{Id=; Name=Service12; Actions=S...

The result I'm after is: Name=Service - 1, Name=Service - 2, etc. I see what's happening, but I'm not sure the proper way to get what I'm after.

Any help is greatly appreciated.

2 Answers 2

1

In your function Copy-Steps you need to actually copy steps:

function Copy-Steps
(
  $StepType,
  $StepCount
)
{
  $OriginalStep = Get-StepType -StepType $StepType 

  1..$StepCount | % {

    $Step=$OriginalStep.PSObject.Copy()
    $Step.Id = ''
    $Step.Name += $_

    # Add step to hash
    $steps.Add("step$($steps.Count + 1)", $step) 
  }  
}
Sign up to request clarification or add additional context in comments.

Comments

1
1..5 | % { [String]::Format("Step {0}", $_) }

or thereabouts

1 Comment

Same result. Removed the inner foreach to simplify a little: 1..$StepCount | % { $Step.Id = '' $Step.Name = [String]::Format("$($StepType) - {0}", $_) $steps.Add("step$($steps.Count + 1)", $step) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.