Powershell SQL query--connection string
Posted
by
sean
on Super User
See other posts from Super User
or by sean
Published on 2012-08-28T14:59:46Z
Indexed on
2012/08/28
15:41 UTC
Read the original article
Hit count: 271
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()
}
© Super User or respective owner