Passing arguments to powershell script executed from c# application is not working
-
יום שלישי 24 אפריל 2012 17:21
I have a bit of code in c# that calls a powershell script. This code for the most part is working fine. However I have made some changes in order to pass parameters into my script. This however is not working for some reason. Heres' some code.
System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); using (System.Management.Automation.RunspaceInvoke runspaceInvoker = new System.Management.Automation.RunspaceInvoke(runspace)) { runspaceInvoker.Invoke("Set-ExecutionPolicy RemoteSigned"); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { string accessScriptPath = System.Configuration.ConfigurationManager.AppSettings["MtpAccessScriptPath"]; string mtpXmlScriptPath = System.Configuration.ConfigurationManager.AppSettings["MtpXmlDestinationPath"] + "\\MtpToPertrac.xml"; string powershellScriptPath = System.Configuration.ConfigurationManager.AppSettings["MtpPowershellScriptPath"]; powershell.Runspace = runspace; powershell.Commands.AddScript(powershellScriptPath); powershell.Commands.AddArgument(mtpXmlScriptPath); powershell.Commands.AddArgument(accessScriptPath); Collection<System.Management.Automation.PSObject> results = new Collection<System.Management.Automation.PSObject>(); Collection<System.Management.Automation.ErrorRecord> errors = new Collection<System.Management.Automation.ErrorRecord>(); try { results = powershell.Invoke(); errors = powershell.Streams.Error.ReadAll(); } catch (Exception ex) { results.Add(new System.Management.Automation.PSObject((object)ex.Message)); } runspace.Close();param ( [string]$mtpXmlFilePath = "<default path>\file.xml", [string]$mtpAccessPath = "<default path>\accessFile.accdb" ) if($env:Processor_Architecture -ne "x86") { write-warning "Running x86 Powershell ...." &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line exit } Set-Psdebug -strict # xml reader [xml]$x = Get-Content $mtpXmlFilePath $newData = $x.ArrayOfMtpToPertracModel.MtpToPertracModel | Select-Object K2ID, HedgeFundLegalEntityName, Mtd, Period, AsOfDate, Estimate $dataSource = $mtpAccessPath $dsn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=$dataSource;" $objConn = New-Object System.Data.OleDb.OleDbConnection $dsn $objConn.Open()
כל התגובות
-
יום חמישי 26 אפריל 2012 06:37מנחה דיון
Hi dharric72,
Welcome to the MSDN forum.
Based on my research, I think you can try the below threads which discussing the similar issue. If you have any further question please let me know.
How to call Powershell Script function from C# ?
Passing parameters to ps1 file
Running powershell from C# Issue
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/463001f1-51be-40a9-8b6b-70c0750f51fa
Have a nice day.
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
-
יום שלישי 01 מאי 2012 20:41Thanks but that does not help me. Basically I have to somehow pass my parameters to the WINDIR\syswow64\windowspowershell\v1.0\powershell.exe line. Anyone know how to do this?
-
יום רביעי 02 מאי 2012 10:03
How about this http://stackoverflow.com/questions/2594587/passing-parameters-to-powershell-from-c-sharp
- סומן כתשובה על-ידי dharric72 יום שלישי 08 מאי 2012 03:11