I have a cmd file which calls a powershell script which displays the input.
The input to the cmd is a list of filenames which it forwards it to the powershellscript which accepts a string array.
When trying it out, the whole list of filenames goes as one parameter.
I tried the answers at the link here and here but no luck.
Below is the output when I run the cmd.
C:\Users\User1>C:\Sample.cmd "C:\file1.txt C:\file2.txt"
Processing file - C:\file1.txt C:\file2.txt
Unfortunately the input to the cmd(list of files) are received from an external program that invokes it.
The powershell script goes like this:
param
(
[Parameter(Position = 0, Mandatory = $true)]
[string[]] $sourceFiles
)
Function Sample_function
{
Param
(
[Parameter(Position = 0, Mandatory = $true)]
[string[]] $sourceFiles
)
foreach($file in $sourceFiles)
{
Write-Host "Processing file - $file"
}
}
Sample_function $sourceFiles
And the cmd goes like this:
@echo off
set PS_File="C:\Sample.ps1"
powershell -FILE "%PS_File%" %*
CodePudding user response:
In order to make an array parameter work with %*
, use the ValueFromRemainingArguments
setting:
param
(
[Parameter(Position = 0, Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]] $sourceFiles
)
Now PowerShell will correctly bind all expanded argument values to $sourceFiles
even though they're separated by space and not ,