PowerShell Script using array as parameter - How?

Discussion and questions - donor version.
Post Reply
Message
Author
fcx64
Posts: 3
Joined: 11.09.2015, 09:04
Location: Germany

PowerShell Script using array as parameter - How?

#1 Post by fcx64 » 28.09.2023, 12:00

Hi. How do I get an array to my PowerShell script? I tried a lot and this command line is the closest I get. It works for 1 selected item.
Multiple Items don't work.
I know that PowerShell wants an array as @() but this didn't work either.


Output of %Dlg%

Code: Select all

-noe -nol -file "F:\-Test.ps1" -Babbel ("F:\TEMP\File 001.txt","F:\TEMP\File 002.txt","F:\TEMP\File 003.txt","F:\TEMP\File 004.txt","F:\TEMP\File 005.txt")
Thank you.

Image


Image

fcx64
Posts: 3
Joined: 11.09.2015, 09:04
Location: Germany

Re: PowerShell Script using array as parameter - How?

#2 Post by fcx64 » 28.09.2023, 12:06

I forgot the script, sorry.

Code: Select all

Param (
    [Parameter(Mandatory = $True)]
    [System.String [] ] $Babbel
)

foreach ($b in $Babbel) {
    Write-Host "$b`n"
}

Read-Host

User avatar
Forez
Posts: 1335
Joined: 30.01.2016, 12:05
Location: In front of a PC monitor

Re: PowerShell Script using array as parameter - How?

#3 Post by Forez » 13.10.2023, 19:09

Perhaps you have run into similar kind of limitation / bug that I have: https://freecommander.com/forum/viewtopic.php?p=41735?

TK87
Posts: 7
Joined: 25.01.2023, 10:02
Location: Germany

Re: PowerShell Script using array as parameter - How?

#4 Post by TK87 » 16.10.2023, 23:44

Hi there.
fcx64 wrote: 28.09.2023, 12:00 How do I get an array to my PowerShell script? I tried a lot and this command line is the closest I get. It works for 1 selected item.
Multiple Items don't work.
I know that PowerShell wants an array as @() but this didn't work either.
Your problem actually has nothing to do with FreeCommander. Windows has the restriction that all programs are executed as the CMD would call them.

You cannot pass an array as a single argument, otherwise it would always be interpreted as a single contiguous string. One possibility would be to split this string inside the script.
The better solution would be to use the advanced parameter "ValueFromRemainingArguments".

Code: Select all

Param (
    [Parameter(Mandatory, ValueFromRemainingArguments)]
    [System.String[]] $Babbel
)

foreach ($b in $Babbel) {
    Write-Host "$b`n"
}
See about_Functions_Advanced_Parameters

For example, if you use %ActivSel% in FreeCommander, you can select multiple files and run the script
Image

Regards, Thomas

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests