Reputation: 964
In my PowerShell script I'm calling a command that takes a file path as a parameter and writes its output to that file path.
For example, the command would look like this C:\mycommand.exe D:\test.csv
, where D:\test.csv will be the new file created by the command.
And in Powershell, I'm calling that command like this $test = & "C:\mycommand.exe" D:\test.csv
.
My intention is that instead of creating the D:\test.csv file, the csv output from the command would be captured by the $test variable and not create the csv file at all.
Instead, what I see happening is that the file is created and $test is empty.
Is there a variable that can act as a file stream that I can pass to the command that would capture the output of the command? Or a way to redirect to a variable without writing the file?
Upvotes: 0
Views: 462
Reputation: 8367
This will depend on the executable.
Some executables can return structured data over standard output. Others will not, and can only output to a file such as you've done.
If you can share the name of the executable, we might be able to see if it can output structured data over standard output.
Otherwise, to get the behavior you'd like from your PowerShell script, you can use the presence of the file, $?
, or $lastExitCode
to determine if the executable ran as expected.
For example:
$outPath = "D:\test.csv" # define your output path
$test = & "C:\mycommand.exe" $outPath
if ((Test-Path $outPath)) { # If the file exists
Import-CSV $outPath # import its contents as .CSV
}
Please share the name of your executable and you may get a better answer.
Upvotes: 1