Hi,
It have tried to call a program from within a script, with parameters stored in variables, but nothing works. For testing and explanation, I made a very simple script:
Get-Childitem is a cmdlet, not a program. There is no reason to use the invocation operator. This is very different than your last post calling an external program. With cmdlets, itâs best to splat the parameters. First create a hashtable with the parameters and arguments
The redirection of stderr to error log is NOT a parameter/argument for cmdlets or external programs so shouldnât be considered as such. I think it might be best to hear what youâre actually trying to do as your example today doesnât make sense.
Sorry my posting made no sense - I am an absolute powershell beginner.
What I want is a script to run a program with several parameters, to redirect the error output to a file; and the parameters / error filename shall be stored beforehand in variables. It works if I enter the command directly in the Powershell:
and it will work in a standard batch(cmd)-file with variables
set prog="c:\program files\Notepad++\notepad++.exe"
set p1=readme.txt
set p2=-n4
set p3=-c5
set p4=2>>err.txt
%prog% %p1% %p2% %p3% %p4%
but I do not manage to create a command like that in a PowerShell script. I tried several possibilities, the most obvious for me would be to do it exactly as in the batch file:
I think the thing that is producing the error is Notepad++; I donât think thatâs the actual error message though. i think itâs actually Cannot Open File D:\2>>D:\err.txt based on my reproduction of the issue.
I donât use legacy apps much like this via cmd line these days but it seems to be itâs not properly referencing the path when you include the error stream redirect/append to the file.
If interested in PS, you may want to give Start-Process a go?
Thank you both for replies, both proposed solutions appear to work. @matt-bloomfield : Interesting, but also very confusing⌠@dotnVo : This appears more logical, and I will use the call via Start-Process.
So I am happy now, but I would be even happier if I had understood why what I tried did not work (i.e. why entering a command directly works, but using variables not). Anyway, it is probably not worth investigating further, now that I have a solution.