question

anupvtr avatar image
0 Votes"
anupvtr asked anupvtr commented

SCOM Custom Monitor: Calling a SQL script using powershell.

Hi All,

I need to run a sql script called by powershell in SCOM monitor. The sql script will perform the activity in DB and the result needs to be captured by powershell property bag.

We are using a community based powershell MP. I will create a powershell based monitor powershell script will called the SQL script. Can anybody help me on this.

Thanks,
Anu

msc-operations-manager
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered anupvtr commented

Hi Anu,

I wrote a blog post on how to integrate a Powershell script in SCOM, using CookDown's PowerShell MP. It has some detailed explainations and also screenshots:

Monitoring Active Directory User Account Expiration using SCOM and PowerShell (Step by Step Guide)
https://www.pohn.ch/monitor-active-directory-user-account-expiration-using-scom-and-powershell-step-by-step-guide/

One more example of how a Powershell script can be used to configure a monitor in SCOM is found in the video:

SCOM PowerShelll Monitoring MP
https://youtu.be/--wOzKU52JE

So, as already mentioned, the article will help you integrate your script. The interesting part is actually the script itself, which needs to make a SQL connection, run a qquery on the DB and get the results back. I can surely help you with the SQL part also. Some years ago I wrote an article on qerying SQL databases, using Powershell (in SMA) and it contains some nice example, which you can use to build your script:

Automating SQL operations with Service Management Automation (SMA) and Invoke-Sqlcmd – challenges and solutions
https://social.technet.microsoft.com/wiki/contents/articles/40091.automating-sql-operations-with-service-management-automation-sma-and-invoke-sqlcmd-challenges-and-solutions.aspx

Here an example of querying and exporting data from a sql table:

 #SQL parameters
 [string] $SQLServer= "Server1"
 [string] $SQLDatabase = "ExampleDB"
 [string] $SQLQuery= $("SELECT AssetStatus, AssetID, Model, Name, Site, ManufacturerName, Type FROM Assets WHERE AssetStatus='3' AND Type='Client' AND Category = 'Hardware'")
 [string]$CSVPath = "\\FileServer\FileShare\Export.csv"
      
 $Command = New-Object System.Data.SQLClient.SQLCommand
 $Command.Connection = $Connection
      
 $SQLConnection = New-Object System.Data.SqlClient.SqlConnection
 $SQLConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDatabase; 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
 $SQLDataSet = New-Object System.Data.DataSet
 $SQLAdapter.Fill($SQLDataSet)
      
 $SQLDataSet.Tables[0] | Export-Csv -Delimiter ';' -NoTypeInformation -LiteralPath $CSVPath -Force


This should help you create your Powershell script.


(If the reply was helpful please don't forget to upvote and/or **accept as answe**r, thank you)

Regards
Stoyan Chalakov





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @StoyanChalakov

Thanks for the help.

Regards,
Anu

0 Votes 0 ·
XinGuo-MSFT avatar image
0 Votes"
XinGuo-MSFT answered AndrewTabar-2806 commented

Hi,

Found a document which seemed helpful for our question, please refer to the link below:

Example of setting up a custom monitor in SCOM with a Powershell Script


Note: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @XinGuo-MSFT

Thanks for your time. I more interested to call a sql script using powershell.

Regards,
Anu

0 Votes 0 ·

The "Sample Management Pack" that's referenced in that link and required to do a Powershell script as a monitor doesn't exist at the link.

0 Votes 0 ·
StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered anupvtr commented

Hi @anupvtr,

may I ask why you insist on using SQL within a PowerShell script? If there is no specific reason for that I would reccommend you this MP, which will allow you to create a monitor in SCOM for any SQL qery:

Free MP to create SCOM monitors from any SQL query
https://squaredup.com/blog/free-mp-to-create-scom-monitors-from-any-sql-query/

There is even a video in this article, which will show you how to achieve the configurations in SCOM.

If you still insists on going the PowerShell way, I could summrize the steps needed, please just let me know.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Stoyan Chalakov




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @StoyanChalakov ,

Thanks for the valuable input. Could you please summarize the steps needed for powershell.

Thanks
Anupvtr

0 Votes 0 ·