-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSUP_8615.ps1
30 lines (24 loc) · 1.55 KB
/
SUP_8615.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#entering the basic connection info like what server and database
$server = Read-Host -Prompt 'Enter the server you want to query (ex. srvnj11)'
$db = Read-Host -Prompt 'Enter the database you want to query (ex. ACME_Hotfix) - spelling matters'
$table = Read-Host -Prompt 'Enter the table you want to query:'
#please ensure that the fields you want to query are in a csv file and only ONE field per row
$inputField = Get-Content 'C:\Users\vmaster\Desktop\input.csv' | ForEach-Object {$_ -replace ",", "" `
-replace "'", "" `
-replace '"', "" }
#query the database with a user provided table
for($i = 0; $i -lt $inputField.Length; $i++){
$sql = New-Object System.Data.SqlClient.SqlConnection
$sql.ConnectionString = "Server = " + $server + "; Database = " +$db +"; Integrated Security = True"
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "##########################"
$sqlCmd.Connection = $sql
$sqlCmd.CommandTimeout = 600
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlAdapter.SelectCommand = $sqlCmd
$dataSet = New-Object System.Data.DataSet
$sqlAdapter.Fill($dataSet)
$dataSet.Tables[0] | Export-Csv -Path 'C:\Users\vmaster\Desktop\output.csv' -NoClobber -Append #Out-File -FilePath 'C:\Users\vmaster\Desktop\output.txt' -Append -NoClobber
#$sqlCmd | Out-Host
$sql.Close()
}