Blog
By Pete Zerger on 4/11/2010 5:11:43 PM • Rank (16859) • Views 17005
0

0

Someone sent me a couple of fairly complex examples of how to connect to a SQL database remotely and retrieve a dataset. Here's a simpler example of the same operation. Would be quite easy to incorporate this into a function in a larger script (read SCOM Health  Check Script) to execute a  number of queries and output data into a text file.

Sample Script

This script provides easy variables to add server name, db name and your SQL query

  1. Save in notepad with a .ps1 extension
  2. Change the server name and query to desired value
  3. Save and run from a PowerShell prompt
$SQLServer = "MySQLServer" #use Server\Instance for named SQL instances! 
$SQLDBName = "MyDBName"
$SqlQuery = "select * from authors WHERE Name = 'John Simon'"

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

$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)

$SqlConnection.Close()

clear

$DataSet.Tables[0]

 

Conclusion

Seems like this could be the basis to get this OpsMgr health check script in motion we talked about awhile back???

Requirements for a SCOM 2007 Health Check Script?

Comments (5) - Comment RSS
Stefan Koell wrote: on Feb 18, 2010 10:23 AM
Pete, you rock, dude!



I just wanted to fire up google to find an example like this. I am in the process to write a monitor based on a SQL query with the PowerShell module. I think you read my mind and spared me all the trouble stealing and compiling from some sample code.



Perfect timing! Thanks
Pete Zerger wrote: on Mar 09, 2010 06:54 PM
Hey Stefan, have you done anything with the SQL queries in PowerShell yet? I have not spent a lot of time parsing multi-record datasets and was wondering what you've figured out to this point.
Stefan Koell wrote: on Mar 10, 2010 03:19 AM
Hi Pete,

your script was very helpful getting me started! Thanks again. Here's a trimmed version what I do right now:



$SQLServer = "hugo"

$SQLDBName = "somedb"

$SqlQuery = 'EXEC [dbo].[usp_mystoredprocedure] ''' + $parameter1 + ''', ''' + $parameter2 + ''''

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

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

$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)

$SqlConnection.Close()



foreach ($row in $DataSet.Tables[0].Rows)

{

$Drive = $row[1].ToString().Trim()

$Threshold = $row[2].ToString()

$MountPoint = $row[3].ToString().Trim()

}



This thing is used for our custom DB disk space monitoring and our DBA's are putting their thresholds for servers/cluster resources in a table and our monitoring script is fetching those values and checking the disk space (hence the $Drive, $Threshold, ... stuff).



cheers,

Stefan
Stefan Koell wrote: on Mar 10, 2010 03:20 AM
Ups,

looks like formatting is not great in the comments. If you need the script as attachment, let me know...
Bryan wrote: on May 21, 2010 12:44 PM
I recently finished up related script, to connect to a remote ODBC data source. It's part of a work in progress to check the status of Ops Mgr generated tickets in Remedy, and then close alerts for associated resolved tickets (we don't have a 2-way connection [yet?]).



I'm sharing here in case others in the SCC community might also find it helpful:

http://bryandady.squarespace.com/journal/2010/5/21/new-powershell-scripts-to-share.html


Who Viewed
Who Reviewed
Categories
Related Pages
Shortened URL
http://tinyurl.com/yzb7ujf

Top Contributors
Featured Members
Pete Zerger
Points: 65622
Level: System Center Expert
Tommy Gunn
Points: 42748
Level: System Center Expert
Simon Skinner
Points: 40804
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27734
Level: System Center Expert