title: How to run Powershell file with ODP dependency in background of C# console app?
body: <p>I've one PowerShell script file which contains ODP assembly to connect to oracle database. I want to run it in the background of C# console application then display the result in the C# console. This is the PowerShell script:-</p>
  
  <pre><code>Add-Type -path "C:\path\Oracle.DataAccess.dll"
  $constr = "User Id=userxxx;Password=xxxx;Data Source=xxxx"
  $conn= New-Object Oracle.DataAccess.Client.OracleConnection($constr)
  $conn.Open()
  $sql="SELECT A,B,C,D FROM XXXXX"
  $command = New-Object Oracle.DataAccess.Client.OracleCommand( $sql,$conn)
  $reader=$command.ExecuteReader()
  $columnNames=$reader.GetSchemaTable() | Select-Object -ExpandProperty ColumnName
  $resultSet=@()
  while ($reader.Read()) {
      $result=New-Object object
          $result | Add-Member -NotePropertyName $columnNames[0] -NotePropertyValue $reader.GetDateTime(0)
          $result | Add-Member -NotePropertyName $columnNames[1] -NotePropertyValue $reader.GetDateTime(1)
          $result | Add-Member -NotePropertyName $columnNames[2] -NotePropertyValue $reader.GetDateTime(2)
          $result | Add-Member -NotePropertyName $columnNames[3] -NotePropertyValue $reader.GetDateTime(3)
      $resultSet += $result
  }
  $conn.Close()
  $resultSet | Format-Table -AutoSize 
  Read-Host -Prompt “Press Enter to exit”
  </code></pre>
  
  <p>I tried this code using C# to use the PowerShell script:-</p>
  
  <pre><code>RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
  runspace.Open();
  RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  Pipeline pipeline = runspace.CreatePipeline();
  Command myCommand = new Command(@"C:\path\connectDB.ps1");
  pipeline.Commands.Add(myCommand);
  pipeline.Commands.Add("Out-String");
  Collection&lt;PSObject&gt; results = pipeline.Invoke();
  runspace.Close();
  StringBuilder stringBuilder = new StringBuilder();
  foreach (PSObject obj in results)
  {
      stringBuilder.AppendLine(obj.ToString());
  }
  
  Console.WriteLine(stringBuilder.ToString());
  Console.ReadLine(); 
  </code></pre>
  
  <p>But I got this exception: <code>An unhandled exception of type 'System.Management.Automation.CmdletInvocationException' occurred in System.Management.Automation.dll</code></p>
  
  <p>And this is the message: <code>Additional information: Could not load file or assembly 'file:///C:\path\Oracle.DataAccess.dll' or one of its dependencies. An attempt was made to load a program with an program with an incorrect format.</code></p>
  
  <p>How can I view the data, PowerShell file name, username and database used for this in the C# console screen?</p>