Console to script

Dear forum users,
I’m sure this has been asked thousands of times, but I can’t either find the right thread or read it right.

I’m trying to call an exe, with some arguments.
Doing it in powershell 7 behaviors normally (my exe correctly launches with correct args), but in a script, I can’t get it to work (exe starting with bad args (can’t tell which as there is no output, just the help windows showing up)).

#In console
."D:\David\kCsvConverter.exe" --format 9 --csv "D:\David\a folder\1-dessous.csv" --out "D:\David\a folder\1-dessous.stl"
#In script
$Command = "D:\David\kCsvConverter.exe"
Get-ChildItem $Folder\*.csv | Foreach-Object {
	$NewName = $_ -replace ".csv", ".stl"
	$params = @(
			 $Format
			 "$OptionFile `"$_`""
			 "$OutFile `"$NewName`""
		)
	& $Command $params 
}

using a simple Write-Host $Command $params doesn’t show any differences.

I’m just lost with escaping something.

Help appreciated.

These are completely different commands. In your first example. you have --format, --csv, and --out which are completely missing in the second. I would assume you’re trying to populate those with the variables $Format, $OptionFIle, and $OutFile but since you fail to show how those are created/what they hold, it would be only that, a wild guess. The next difference is you have hard coded paths in your first example, in the second you have variables. You should first try to turn your working command into your second example before changing/adding variables. Once you know you have the structure working, then you can move on to populate with variables.

So first, we are going to break your original command down

# . "D:\David\kCsvConverter.exe" --format 9 --csv "D:\David\a folder\1-dessous.csv" --out "D:\David\a folder\1-dessous.stl"

$command = "D:\David\kCsvConverter.exe"

$arguments = '--format',
             '9',
             '--csv',
             '"D:\David\a folder\1-dessous.csv"',
             '--out',
             '"D:\David\a folder\1-dessous.stl"'

& $command $arguments

If that works fine, then we can tackle populating those arguments dynamically.

for some reasons, Start-Process instead of & makes the job.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.