title: Powershell connection to SQL server database in another domain using integrated security
body: <p>I'm trying to write a powershell script to connect to a server in another domain using integrated security. This is the only way we can connect to our MS SQL server database due to a policy restriction. Right now I run it using SSMS by holding down shift and right clicking the SSMS shortcut, choosing run as a different user and put in my other domain credentials. Is there any equivalent option in powershell? I'm still using Powershell v 1.0 on a win7 64 bit vm. The sql server is ms sql 2k8 r2. Here is my code so far.</p>
  
  <pre><code>$dataSource = "ProdDomain"
  $database = "MyProdDatabase"
  $connectionString = "Server=$dataSource;Database=$database;Integrated Security=true;"
  $query = "SELECT mycolumn FROM mytable"
  $connection = New-Object System.Data.SqlClient.SqlConnection
  $connection.ConnectionString = $connectionString
  $connection.Open()
  $command = $connection.CreateCommand()
  $command.CommandText = $query
  $result = $command.ExecuteReader()
  $table = new-object “System.Data.DataTable”
  $table.Load($result)
  $table | format-table 
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Exception calling "Open" with "0" argument(s): "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication."
        At line:9 char:1
        + $connection.Open()
        + ~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                + FullyQualifiedErrorId : SqlException</p>
  </blockquote>