Array not working

Hi there,

I am running a reasonably simple script to build an array and power shell and it is not working when I run it in Octopus. Here is the script:

Initialize an empty array to store objects

$primaryArray = @()
$files = @(“C:\Experimentation\test-cluster”,“C:\Octopus”)

Get folder names matching 6 digit numbers

$pattern = ‘^\d{6}$’
Foreach ($f in $files) {
$folders = Get-ChildItem -Path “$f” | Where-Object ({ $.Name -match $pattern } -and { $.PSIsContainer })

Assign Variables for each folder

foreach ($f in $folders) {

$foldername = $f.name
$location = $f.fullname

Creat object with variables

$objectData = @(
@{ staffID = “$foldername”; location = “$location” }
)

Iteratively add object to the array using foreach loop

foreach ($data in $objectData) {

Check if an object with the same properties already exists in the array

$existingObject = $primaryArray | Where-Object { $.staffID -eq $data.staffID -and $.location -eq $data.location }

If the object doesn’t exist, add it to the array

if (-not $existingObject) {
$newObject = New-Object PSObject -Property $data
$primaryArray += $newObject
}
}
}
}

Display the objects in the array

$primaryArray

$primaryArray | Export-Excel -Path “C:\Experimentation\primaryArray.xlsx” -FreezeTopRow -AutoSize

New-OctopusArtifact “C:\Experimentation\primaryArray.xlsx”

I have run the script on the machine (Target node) directly and it functions perfectly producing an array with three items, however when I run it in Octopus Deploy it produces no result whatsoever.

It almost seems like the arrays don’t function, however I have run the following script is a test of this:

$foldername = “Dave”
$location = “Dinnison”

Sample data - you can modify this to add your own objects

$objectData = @(
@{ staffID = “$foldername”; location = “$location” }
)

Iteratively add objects to the array using foreach loop

foreach ($data in $objectData) {

Check if an object with the same properties already exists in the array

$existingObject = $primaryArray | Where-Object { $.staffID -eq $data.staffID -and $.location -eq $data.location }

If the object doesn’t exist, add it to the array

if (-not $existingObject) {
$newObject = New-Object PSObject -Property $data
$primaryArray += $newObject
}
}

Display the objects in the array

$primaryArray

And it works as expected and produces an array…

Any help solving this would be much appreciated

ServerTasks-4910395.log.txt (6.9 KB)

Here are the logs on the failing runbook

Nevermind, I figured it out:

-and { $ .PSIsContainer })

it didn’t like this

1 Like

Hi @bnz_technology_tooling_ops
Thanks for the question and also for posting your resolution. Helps everyone who might need this to have the resolution also.

Kind regards,
Paraic

1 Like