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
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:
Set the value of your variable to filepath1.txt,filepath2.txt (see attached screenshot)
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.
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.
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