Powershell script with input variable as string array

I have added a variable called myFiles with a value of @(“filepath1.txt”,“filepath2.txt”) to the variables page. It is an input parameter for a script module function
ReadArray{
[CmdletBinding()]
Param
( [Parameter(Mandatory=$True)]
[string[]] $Files )
foreach($file in $Files){
Write-Host "File path : $file
## Retrieve the filename and path into separate variables
$FileInfoObject = Get-ChildItem -Literalpath $file
$Filename = $fileInfoObject.Name
$Path = $fileInfoObject.DirectoryName
}
}
This function is executed in a process step as
ReadArray -Files $myFiles
The the Write-Host "File path : $file returns @(“filepath1.txt”,“filepath2.txt”) instead of filepath1.txt

Is there some special syntax that I can add to the variable value so that the function realizes that the $myFiles is an array?
Thanks,
Chieko

Hi Chieko,

Thanks for getting in touch! Octopus treats all variables as strings, so to create an array out of an Octopus variable you’re gonna have to do some extra work:

  1. Set the value of your variable to filepath1.txt,filepath2.txt (see attached screenshot)

  2. Put this code snipped before you use the $myFiles array:

$myFiles = $myFfiles.Replace("'","").Split(",")

This will leave you with $myFiles as an array containing 2 times.

Hope that helps!

Dalmiro

Hi Dalmiro,
Thank you for you quick response.
Unfortunately, the code $myFiles = $myFfiles.Replace("’","").Split(",")
returned an Method invocation failed because [System.String[]] doesn’t contain a method named ‘Replace’.

I think I understand what I have to do though.
I’ll ‘echo’ the $Files variable and see what I need to replace in order for powershell to identify it as an array.
I’ll let you know if I still need help.

Thanks,
Chieko Jurkus
X4031

Hi Chieko,

My code had a typo (an extra F on $myFiles), apologies for that :(.

The line should be:

$myFiles = $myFiles.Replace("'","").Split(",")

Please try again!

Dalmiro

Yes, I corrected the typo, but it still failed.

Chieko Jurkus
X4031

Hi Chieko - This is happening because you are declaring $files as an array of strings at the beginning of your function. Remember that you’ll actually be passing a single string, and then inside your function you’ll be converting it to an array of strings. Here’s how your function should look like

function ReadArray{ 
[CmdletBinding()] 
Param 
( 
    [Parameter(Mandatory=$True)] 
    [string] $Files #Getting the files as a single string
) 

    [string[]]$Files = $Files.Replace("'","").Split(",") #converting the single string into an array of strings

    foreach($file in $Files){ 
                        Write-Host "File path : $file "
                            
                            
                        # Retrieve the filename and path into separate variables 
                        $FileInfoObject = Get-ChildItem -Literalpath $file 
                        $Filename = $fileInfoObject.Name 
                        $Path = $fileInfoObject.DirectoryName 
    } 
} 

And then call the function like this
ReadArray -Files $myFiles

Thanks

Dalmiro

Thank you. your more descriptive example works great.

Chieko Jurkus
X4031