Function New-SQLDBConnection {
Param ([string]$server,
[string]$db,
[string]$connectionName = $ScriptName)
if (test-path variable:\conn) {
$conn.close()
} else {
$conn = new-object ('System.Data.SqlClient.SqlConnection')
}
$connString = "Server=$server;Integrated Security=SSPI;Database=$db;Application Name=$connectionName"
$conn.ConnectionString = $connString
$conn.StatisticsEnabled = $true
$conn.Open()
$conn
}
Since we are not needing to access any data from any specific databases, we will move on to the second function.
function New-SQLInstanceConnection {
Param ([string]$server,
[string]$connectionName = $ScriptName)
if (test-path variable:\conn) {
$conn.close()
} else {
$conn = new-object ('System.Data.SqlClient.SqlConnection')
}
$connString = "Server=$server;Integrated Security=SSPI;Database=$db;Application Name=$connectionName"
$conn.ConnectionString = $connString
$conn.StatisticsEnabled = $true
$conn.Open()
$conn
}
In this function, we are only providing the server instance name. The function will use that information and establish your connection.
function Query-SQL {
Param ($query, $conn, [int]$CommandTimeout = 30)
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandTimeout = $CommandTimeout
$sqlCmd.CommandText = $query
$sqlCmd.Connection = $conn
$Rset = $sqlCmd.ExecuteReader()
if ($? -eq $false) {
Close-SQLquery $rset
[int]$rset = -1
}
if ($Rset.hasrows -eq $false) {
Close-SQLquery $rset
[string]$rset = ""
}
,$Rset
}
You must pass the actual query and the connection that was established by the previous function to this function. This function will then return the results of the query in a format you can code against.
function Close-SQLquery {
Param ($query)
$query.close()
$query = $null
}
function Remove-SQLconnection {
Param ($connection)
$connection.close()
$connection = $null
}
$sqlServers = import-csv "c:\scripts\Get-SQLServerInfo\SQLServers.csv"
for ($i = 0; $i -le ($sqlServers.Length -1); $i +=1)
{
$sqlServer = $sqlServers[$i].SqlServer
$myConn = New-SQLInstanceConnection "$sqlServer"
if ($x.state -eq "closed") {"Failed to establish a connection";EXIT}
$query = "SELECT @@VERSION"
$data = Query-SQL $query $myConn
foreach ($item in $data)
{
$result = $item.GetValue(0)
#you can write the results below out to any format you want. For the purpose of this example, we will just dump them to the screen.
$sqlServer
$result
}
# Close the SQL Query using the Close-SQLquery function
Close-SQLquery $data
# Remove the SQL connection using the Remove-SQLConnection function
Remove-SQLconnection $myConn
}
Function New-SQLDBConnection {
Param ([string]$server,
[string]$db,
[string]$connectionName = $ScriptName)
if (test-path variable:\conn) {
$conn.close()
} else {
$conn = new-object ('System.Data.SqlClient.SqlConnection')
}
$connString = "Server=$server;Integrated Security=SSPI;Database=$db;Application Name=$connectionName"
$conn.ConnectionString = $connString
$conn.StatisticsEnabled = $true
$conn.Open()
$conn
}
function New-SQLInstanceConnection {
Param ([string]$server,
[string]$connectionName = $ScriptName)
if (test-path variable:\conn) {
$conn.close()
} else {
$conn = new-object ('System.Data.SqlClient.SqlConnection')
}
$connString = "Server=$server;Integrated Security=SSPI;Database=$db;Application Name=$connectionName"
$conn.ConnectionString = $connString
$conn.StatisticsEnabled = $true
$conn.Open()
$conn
}
function Query-SQL {
Param ($query, $conn, [int]$CommandTimeout = 30)
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandTimeout = $CommandTimeout
$sqlCmd.CommandText = $query
$sqlCmd.Connection = $conn
$Rset = $sqlCmd.ExecuteReader()
if ($? -eq $false) {
Close-SQLquery $rset
[int]$rset = -1
}
if ($Rset.hasrows -eq $false) {
Close-SQLquery $rset
[string]$rset = ""
}
,$Rset
}
function Close-SQLquery {
Param ($query)
$query.close()
$query = $null
}
function Remove-SQLconnection {
Param ($connection)
$connection.close()
$connection = $null
}
$sqlServers = import-csv "c:\scripts\Get-SQLServerInfo\SQLServers.csv"
for ($i = 0; $i -le ($sqlServers.Length -1); $i +=1)
{
$sqlServer = $sqlServers[$i].SqlServer
$myConn = New-SQLInstanceConnection "$sqlServer"
if ($x.state -eq "closed") {"Failed to establish a connection";EXIT}
$query = "SELECT @@VERSION"
$data = Query-SQL $query $myConn
foreach ($item in $data)
{
$result = $item.GetValue(0)
#you can write the results below out to any format you want. For the purpose of this example, we will just dump them to the screen.
$sqlServer
$result
}
# Close the SQL Query using the Close-SQLquery function
Close-SQLquery $data
# Remove the SQL connection using the Remove-SQLConnection function
Remove-SQLconnection $myConn
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)