Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to query several different SQL servers and run a command on each of them. I am unable to get the connection string right. Code, below.

I receive the following error:Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.

I thought if I passed it the credentials it wouldn't care about the domain. How do I get around this?

Thanks in advance.

$serverList = @(Get-Content "c:\AllServers.txt")
$query = "SELECT COUNT(thing) AS [RowCount] FROM My_table"
$Database = "My_DB"

# Read a file
foreach ( $svr in $serverList )
{
$conn=new-object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};User ID=sa;Password=Password;Integrated Security=True" -f $svr, $Database
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
$conn.Close()
}
share|improve this question
add comment

1 Answer

You need to remove Integrated Security=True from the connection string. If you set that to true it will try to do AD/kerberos authentication through your windows credentials as opposed to the user id and password specified.

Also, please review the SQL 2008 page on connectionstrings.com.

share|improve this answer
    
Thank you. I have tried it with and without the Integrated Securty, along with other permutations of the connection strings on that page. The errors range from:'"Invalid value for key 'network library'."' to '"Keyword not supported: 'provider'."' –  sean Aug 28 '12 at 15:33
    
@Sean please try it without Integrated Security=True, update your question to reflect that exact permutation of the script and and error you get with that permutation. –  Justin Dearing Aug 28 '12 at 15:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.