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