Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, November 13, 2010 11:14 PM
Hi,
I'd like to run a powershell script file from C# code (e.g. C:\myscript.ps1 which has three arguments)
Here is what I have:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.Arguments = "& {& \"C:\\myscript.ps1\" arg0 arg1 arg2}";
Process process = new Process();
process.StartInfo = psi;
process.Start();
The powershell script with its arguments can be run in powershell.exe
Does anyone know how to resolve it?
Any suggestion is greatly appreciated.
All replies (6)
Saturday, November 13, 2010 11:35 PM ✅Answered | 1 vote
So what is the exact problem that you are having?
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://rudedog2.spaces.live.com/default.aspx
Sunday, November 14, 2010 1:00 AM ✅Answered
hi Rudedog2,
Your link indeed is helpful. A great reference for C# and powershell.
It seems my program is working now after a while of 'divide and conquer'.
The cause is very strange. The code in my original post was not correctly posted (as I didn't know the cause).
The original code is like this:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.Arguments = "& \"C:\\my powershell script\\myscript.ps1\" \"C:\\my powershell script\\txt1.txt\" \"C:\\my powershell script\\txt2.txt\" \"C:\\my powershell script\\txt3.txt\"";
Process process = new Process();
process.StartInfo = psi;
process.Start();
The cause was that in Arguments, it doesn't recognise double quotes while in powershell.exe, it does.
The following code works:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.Arguments = "&'C:\\my powershell script\\myscript.ps1' 'C:\\my powershell script\\txt1.txt' 'C:\\my powershell script\\txt2.txt' 'C:\\my powershell script\\txt3.txt'";
Process process = new Process();
process.StartInfo = psi;
process.Start();
I think, the program was somehow confused about passing \ ...
Anyway, hope this is helpful. Lucky me, I could spend another couple of hours if I didn't try that...
Saturday, November 13, 2010 11:37 PM
Hi, thanks.
The problem is that the script does not do its job.
The script should delete some files but I can see the files still exist after the process finishes.
Saturday, November 13, 2010 11:51 PM
I assumed as much. Did you go to the link that I posted? It describes the tools you need and how you use the PowerShell programmatically.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://rudedog2.spaces.live.com/default.aspx
Tuesday, November 29, 2011 8:24 AM
Hi , i am running above code in console application .please tell me how to stop powershell screen in c# console application .actually i got error while running above code in psi.argument.
code i am using is :--
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.Arguments = "&'C:\ajax.ps1'";
Process process = new Process();
process.StartInfo = psi;
process.Start();
Tuesday, January 8, 2013 6:57 PM
Warning -
If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete.
cmd> PowerShell
PS> Start-Process cmd.exe -Wait
Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window:
cmd2> PowerShell
PS> Start-Process cmd.exe -Wait
PS>
The 2nd instance of PowerShell no longer honors the -Wait request and ALL background process/jobs return 'Completed' status.
I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request.
It appears that any PowerShell which is a 'jwin32 ob' of cmd.exe fails to honor the wait request.
none