title: Powershell - SQL Server - connectionstring in loop for multiple Instances
body: <p>Thisis part of a bigger script which finds <em>DatabaseFiles</em> on a SQL Server machine (multiple instances).</p>
  
  <p>Following should just return all files for 3 instances. </p>
  
  <p>The server is called <code>V3000801</code> and there is one default instance + 2 named instances on there. It's ok with me if either default or named doesn't work I'll work around this alone (most likely create a flag and do default with another connection string).</p>
  
  <p><code>$SqlCmd.ExecuteNonQuery()</code> just returns -1 which does not make any sense for me.</p>
  
  <p>Thanks for the help  </p>
  
  <pre><code>for($i=0;$i -lt $instances.Length;$i++){
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
    $Server= "V3000801\"+$instances[$i];
    $SqlConnection.ConnectionString = "Server = $server ; Database = master; Integrated Security = sspi;trusted_connection=true";
    $sqlQuery="SELECT physical_name FROM sys.master_files;";
  
    Write-Host $SqlConnection.ConnectionString;
  
    $SqlConnection.Open();
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
    $SqlCmd.CommandText = $sqlQuery;
    $SqlCmd.Connection = $SqlConnection;
    $SqlCmd.ExecuteNonQuery();
    $SqlConnection.Close();
  }
  </code></pre>