Passing parameters to powershell script in C#


hi, have read lot of posts regarding issue still can't make work

http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments

http://www.codeproject.com/articles/18229/how-to-run-powershell-scripts-from-c

http://social.msdn.microsoft.com/forums/en-sg/csharpgeneral/thread/faa70c95-6191-4f64-bb5a-5b67b8453237

i have windows form 2 textboxes

tb_user

tb_office

i have powershell script on c:\scripts\script.ps1  that need run 2 parameters

script.ps1 -user tb_user.tostring() -office tb_office.tostring()

so getting textbox content parameters call script on button click, 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 add new script 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 name results doesn't exist in current context, , also, nothing runs.. :(

can help?

thanks



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'