I have a PowerShell script where I use the Call operator to run mkvmerge.exe with a set of arguments/options. I wanted to collect the options in a string so that the string could be expanded based on inputs earlier in the script, but when the destination file is in this string mkvmerge does not recognize it for some reason. I am unsure if this is because of variable expansion or what the issue is
The code below illustrates the problem. I have also tried adding the options to an array and using the @ operator, but with no luck
$mkvmerge = "C:\Program Files\MKVToolNix\mkvmerge.exe"
$applyoptions = $true
$subtitleformat = "srt"
$isolanguage = "eng"
foreach ($file in Get-ChildItem -File -Include *.mkv) {
<# Make the string with options for mkvmerge #>
$mkvmergeparams = ""
if ($applyoptions -eq $true) {
$mkvmergeinstructions = ('`' "@options.json")
}
$mkvmergeparams = ("-o " "`"" $file.DirectoryName "\Remux-" $file.BaseName ".mkv`" " "`"" $file "`"")
if (-not($subtitleformat -eq "")) {
$mkvmergeinstructions = (" --default-track " $subtitledefault) " --language " "0:" $isolanguage " " "`"" $file.DirectoryName "\" $file.BaseName "." $subtitleformat "`""
}
<# Check the string #>
Write-Host $mkvmergeparams
<# This does not work, but I would like it to #>
& $mkvmerge $mkvmergeparams
<# This works, but would require a separate line for each set of possible options #>
#& $mkvmerge `@options.json -o ($file.DirectoryName "\" "Remux-" $file.BaseName ".mkv") $file --default-track $subtitledefault --language ("0:" $isolanguage) ($file.DirectoryName "\" $file.BaseName "." $subtitleformat)
}
Read-Host -Prompt "Press enter to exit"
In the second example the variables are expanded and mkvmerge recognizes the options given, but this requires having a separate mkvmerge line for each set of possible inputs
When checking the string it seems to expand to look exactly as it should
CodePudding user response:
Found the answer. I had to create an array with the arguments
<# Build array with options for mkvmerge #>
$mkvmergeparams = $null
if ($applyoptions -eq $true) {$mkvmergeparams = @('@options.json')}
$mkvmergeparams = @("-o", ($file.DirectoryName "\Remux-" $file.BaseName ".mkv"), $file)
if (-not($subtitleformat -eq "")) {$mkvmergeparams = @("--default-track", $subtitledefault, "--language", ("0:" $isolanguage), ($file.DirectoryName "\" $file.BaseName "." $subtitleformat))}