Solved

Integrate TSQL code with Powershell

Posted on 2011-09-14
10
466 Views
Last Modified: 2012-05-12
Hi,

Before doing any activity (Example :Applying Patches) on prod servers, we are MANUALLY doing pre/post verifications on servers. The verifications checklist  includes following items:
1. SQL INSTANCE NAME
2. NO OF DATABASES
3. DATABASE STATUS (ONLINE/OFFLINE)
4. Online Physical Server Name
5. SQL START TIME
6. SERVER UP TIME
7. SQL PRODUCT VERSION/PRODUCT LEVEL/PRODUCT EDITION

Now, we want to do  this activity as automated (With Centralized) through Poweshell scripting & TSQL CODE and results/output should be in the form of CSV file.

I have created stored proc with TSQL and we have to integrate this code with powershell.

I am totally new to Powershell technology, so need help from Powershell Exprts.

Please give your suggestions on above requirement (how to integrate attached stored proc with powershell).

Thanks in Advance.
Pre-Post-Verification-USP.txt
0
Comment
Question by:sg05121983
  • 4
  • 4
10 Comments
 
LVL 19

Expert Comment

by:Rimvis
ID: 36540889
Hi sg05121983,

This might help:

How do I call an Sql Server stored procedure from Powershell?
http://stackoverflow.com/questions/83410/how-do-i-call-an-sql-server-stored-procedure-from-powershell
0
 

Author Comment

by:sg05121983
ID: 36541007
In above link, the CSV details (how to generate output in the form CSV) are not given.

Also, we want to make this activity as centralized not for specific instance.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36556497
Do you want to
run the existing stored procedure on each instance - requires that you create and modify it on each instance;
prefer to have the PS code define the stored procedure each time;
run the included T-SQL code instead directly from PS (not creating or using the stored procedure)
?
If you look at what the stored procedure gives for results, you will see each part is different – retrieving a single value and column, or retrieving more than one column, with different column names (of course), and so on. Long story short, the results are heterogenous, and cannot be stored in a single CSV file that way as rows. Think of the CSV as being like a database table; you would have to use distinct columns for distinct meanings.

The only correct way to handle the output is to create one "record" per server, having all infos in the appropriate "columns". Is that what you are after?
0
 

Author Comment

by:sg05121983
ID: 36559212
Hi Olemo,

Thanks for your reply.

1. My main requirement is to produce the output for single instance.

For Ex:
Server Name is : A1 : When i am running script on this server, i need output of A1 server in CSV format and output should be stored in any drive (A Server drive) or i have to receive O/P through mail (mail configuration).

The procedure (TSQL) is same for all instances.

OR

2. Second requirement (Optional):

Make this task as centralized.

For example, I need results of A10 instance and i am executing PS (integrated with stored proc) on A2 server.

Please help me on this requirement (integrate TSQL stored proc with PS)

Thanks in Advance.

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

Expert Comment

by:Qlemo
ID: 36568436
As I said already, combining the output into a CSV the way the values are returned doesn't make sense. In particular since there is one query returning more than one row (the installed databases) - how should that be processed? Would you want to have all other data repeated for each DB? That would look similar to:
Server1 Version1 DBCount1 Db1.1
Server1 Version1 DBCount1 DB1.2
Server2 Version2 DBCount2 DB2.1
Server2 Version2 DBCount2 DB2.2
Server2 Version2 DBCount2 DB2.3

Open in new window

where DB1.1 and DB1.2 are located on the same server/instance.

If not, I would stick to a text-only output, without further formatting, as it results from the selects.
0
 

Author Comment

by:sg05121983
ID: 36577959
Yes, you are right.

Please share the code  (PS integrated with attached stored proc - output in the form of text-only option).

I will try in my local machine/server and update you the results.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36578824
I'm preparing a piece of PS code which contains the complete T-SQL code - I would favour that instead of the Stored Procedure, since the output of each select needs to be processed seperately anyway in PS. Alternatively you could write the Stored Procedure in a way it already provides the deserialized result, that is one row per DB, each row containing all data related to the server (according to the example output I showed). Example for doing that:
select d1.*, d2.*
from (select ...) d1,
(select ...) d2,
...

Open in new window

0
 

Author Closing Comment

by:sg05121983
ID: 36907706
--
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36908642
Sorry I didn't manage to return to this question with detailed code. Has my comment been sufficient to get you started with an own solution (then it is ok the way the question has been closed), or did you simply want to get rid of the question? Should you not have something reasonable out of this question, we should reopen (I, as Zone Advisor, can do that) and continue.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now