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
Friday, March 1, 2013 2:04 PM
Hi, i have read a lot of posts regarding this issue but i still can't make it to work
http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C
I have a Windows Form with 2 Textboxes
TB_user
TB_office
I also have a powershell script on c:\scripts\script.ps1 that i need to run with 2 parameters
script.ps1 -user TB_user.Tostring() -office TB_office.ToString()
So getting the textbox content as parameters to call the script on a button click, my code is:
private void button1_Click(object sender, EventArgs e)
{
string user = TB_user.ToString();
string office = TB_office.ToString();
var scriptfile = @"c:\scripts\script.ps1";
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile);
CommandParameter userParam = new CommandParameter("user", user);
myCommand.Parameters.Add(userParam);
CommandParameter officeParam = new CommandParameter("office", office);
myCommand.Parameters.Add(officeParam);
pipeline.Commands.Add(myCommand);
// Execute PowerShell script
results = pipeline.Invoke();
}
}
}
I got the name results doesn't exist in the current context, and also, nothing runs.. :(
Can anybody help?
Thanks
All replies (13)
Friday, March 1, 2013 5:31 PM ✅Answered
refer this link
http://www.codeproject.com/Questions/213870/Runnung-Powershell-Scripts-with-aruguments-using-C
and please refer this
http://blogs.msdn.com/b/zainnab/archive/2008/07/26/calling-a-powershell-script-from-your-net-code.aspx //this link give you in detail in step by step
Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights
Friday, March 1, 2013 6:47 PM ✅Answered
Um.. I don't see results
declared anywhere. Hence the error results doesn't exist in the current context.
i.e.:
Collection<PSObject> results = ....
(I can't believe no one else caught this??)
Now whether this is a solution to your problem, I don't know. But if it is, please mark it.
Friday, March 1, 2013 2:17 PM | 1 vote
what if you try to use Process.Start() :
string user = TB_user.ToString();
string office = TB_office.ToString();
var scriptfile = @"c:\scripts\script.ps1";
Process _Proc = Process.Start("Powershell.exe", @"" + scriptfile + " '" + user + "' " + " '" + office + "'");
regards
joon
Friday, March 1, 2013 2:23 PM
Hi, what assembly reference you use for process?
Thanks
Friday, March 1, 2013 2:26 PM | 1 vote
sorry, forgot to mention. It is "using System.Diagnostics;"
regards
joon
Friday, March 1, 2013 2:32 PM
Hi, Thanks.
2 questions, when i run it it starts powershell in a window, can i hide it?
Also it seems powershell returns an error but i can't see where because the window closes...
Also the process shouldn't be like this:?
Process _Proc = Process.Start("Powershell.exe", @"" + scriptfile + "-user" + user + "' " + "-office" + office);
thanks
Friday, March 1, 2013 2:40 PM | 1 vote
you may need to run the powershell with an additional parameter, check this link for this : http://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window
string user = TB_user.ToString();
string office = TB_office.ToString();
var scriptfile = @"c:\scripts\script.ps1";
Process _Proc = new Process();
_Proc.StartInfo = "Powershell.exe";
_Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_Proc.StartInfo.Arguments = "'" + user + "' " + " '" + office + "'";
_Proc.Start();
there is one API available for this work, check this : http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28VS.85%29.aspx
regards
joon
Friday, March 1, 2013 3:01 PM
Hello,
Not related to your problem directly but it might help you to track down the problem, you can use EachoArgs.ps1 to check what are the actual arguments you send and how you sent it by executing the script.
Small script but handy.
Regards,
Friday, March 1, 2013 3:05 PM | 1 vote
I have a Windows Form with 2 Textboxes
TB_user
TB_office
private void button1_Click(object sender, EventArgs e) { string user = TB_user.ToString(); string office = TB_office.ToString();
Your now calling ToString on the TextBox. I would think you want to use TB_user.Text; to get the user input from the TextBox.
Friday, March 1, 2013 3:24 PM
hi where do you call scriptfile from there?
Also the script neds to be called like:
script.ps1 -user user -office office
And right now it is not working, can you please help?
thanks
Friday, March 1, 2013 3:32 PM
I think you ight be right with that so i changed thm to:
string user = TB_user.Text;
string office = TB_office.Text;
But i still don't know how to call the process with the arguments
Thansk
Friday, March 1, 2013 3:43 PM | 1 vote
Hi,
Which method do you want to use ? If RunSpace IMO you should use the AddScript method at some point. Currently your code does as if the script file name was a real PS command...
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
Friday, March 1, 2013 3:58 PM
Hi Patrice, at the moment i want to pass 2 parameters on
startInfo.Arguments = @"c:\script.ps1";
So i get script1.ps1 + -user "user" + -office "office"
Not sure which method is best.
Thanks