programing

PowerShell에서 SQL Server 데이터베이스에 연결

magicmemo 2023. 7. 30. 17:34
반응형

PowerShell에서 SQL Server 데이터베이스에 연결

제가 지금 온라인으로 잠시 둘러보니 비슷한 문제들이 많은데, 왠지 잘 안 되는 것 같습니다.

SQL 서버 데이터베이스에 연결하여 쿼리 결과를 파일로 출력하려고 합니다. 아래 PowerShell 스크립트를 참조하십시오.사용자 ID와 비밀번호를 연결 문자열에 통합하는 방법에 대해 잘 모르겠습니다.

$SQLServer = "aaaa.database.windows.net"
$SQLDBName = "Database"
$uid ="john"
$pwd = "pwd123"
$SqlQuery = "SELECT * from table;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True; User ID = $uid; Password = $pwd;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

$DataSet.Tables[0] | out-file "C:\Scripts\xxxx.csv"

다음 오류 메시지가 수신됩니다.

"1" 인수를 사용하여 "채우기"를 호출하는 중 예외 발생: "이 버전의 SQL Server에서는 윈도우즈 로그인이 지원되지 않습니다.

Integrated Security그리고.User ID\Password인증은 상호 배타적입니다.코드를 실행하는 사용자로 SQL Server에 연결하려면 다음을 제거합니다.User ID그리고.Password연결 문자열에서:

$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"

특정 자격 증명으로 연결하려면 제거Integrated Security:

$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID = $uid; Password = $pwd;"

연결 문자열에서 통합 보안을 false로 변경합니다.

사용자가 가지고 있는 사용자 이름/암호로 SQL 관리 스튜디오를 열고 데이터베이스를 연결/열 수 있는지 확인하여 이를 확인할 수 있습니다.참고! 방화벽 문제일 수도 있습니다.

# database Intraction

$SQLServer = "YourServerName" #use Server\Instance for named SQL instances!
$SQLDBName = "YourDBName"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; 
User ID= YourUserID; Password= YourPassword" 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 'StoredProcName'
$SqlCmd.Connection = $SqlConnection 
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd 
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) 
$SqlConnection.Close() 

#End :database Intraction
clear

Window 인증에 대한 답변은 아래와 같습니다.

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$SQLServer;Database=$SQLDBName;Integrated Security=True;"

통합 보안을 사용할 수 있다고 가정하면 사용자 ID 및 패스를 제거할 수 있습니다.

$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"

SQL Server에 활성화된 디렉토리 사용자로 연결하려면 PowerShell을 활성화된 디렉토리 사용자로 시작하고 다음을 사용하여 SQL Server에 연결합니다.TrustedSecurity=true

통합 보안을 제거했습니다... 제 목표는 활성 디렉토리 사용자 이름/암호가 있는 연결 문자열을 사용하여 SQL 서버에 로그온하는 것입니다.제가 그렇게 할 때는 항상 실패합니다.형식은 중요하지 않습니다... sam company\user...upn whatever@company.com ... 기본 사용자 이름.

문제의 특정 사용자가 SQL 서버 데이터베이스에서 암호를 사용하여 로그인하도록 명시적으로 설정된 경우에만 작동할 수 있습니다. 즉, 통합 윈도우즈/Single-Sign-On에서 자격 증명을 상속하지 않습니다.

언급URL : https://stackoverflow.com/questions/25682703/connect-to-sql-server-database-from-powershell

반응형