locked
Passing arguments to powershell script executed from c# application is not working

    Question

  • 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()


    Tuesday, April 24, 2012 5:21 PM

Answers

All replies