PowerShell로 SQL 쿼리 Etc.2015. 8. 12. 09:20
Param(
[Parameter(Mandatory=$True)]
[string]$SQLServer,
[Parameter(Mandatory=$True)]
[string]$SQLDBName,
[Parameter(Mandatory=$True)]
[string]$UserID,
[Parameter(Mandatory=$True)]
[string]$UserPW
)
$SqlQuery = "SELECT * FROM TABLE_VIEW"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
# $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; uid="+$UserID+"; pwd="+$UserPW
$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) | Out-Null
$SqlConnection.Close()
foreach ($row in $DataSet.Tables[0].Rows) {
$Row1 = $row[0].ToString().Trim()
$Row2 = $row[1].ToString().Trim()
$Row3 = $row[2].ToString().Trim()
Write-Host $Row1, $Row2, $Row3
}