This is part two in my PowerShell and SQL series. If you missed part one, you can use the PowerShell link under labels on the right to get to it. Last week I went over PowerShell Basics, what it is and how to use it. I included some examples to show you how you can use PowerShell to get SQL related event logs and store them in a file. This week I am going to introduce the SQL Server PowerShell Extensions.
You can download the modules for SQLPSX from the codeplex website: http://sqlpsx.codeplex.com/
SQLPSX is a library of modules that can be used to directly talk to SQL server. You can get server and database information, read from and write to a SQL database and even perform most tasks a DBA performs on a regular basis. Now you can do this without using SQLPSX by creating your own connections to a SQL server but I am going to show you why you shouldn't bother reinventing the wheel.
Installation
Once you download the file and run it, the SQLPSX modules will be added to your modules sub-directory of your Windows PowerShell folder (usually located under My Documents). In order to use the modules you have to import them into PowerShell.NOTE: loaded modules are session based, this means that when you close the PowerShell window and get back in, the modules will not be loaded anymore. You can have them auto-load when PowerShell opens by adding an entry to your PowerShell profile. If you are interested here is an article at How-to-Geek: http://www.howtogeek.com/50236/customizing-your-powershell-profile/
You load the module into powershell using the import-module cmdlet, but wait, do you know what the names of the modules are? After you have installed SQLPSX open up powershell and type this command:
Get-Module -ListAvailable
You will get a result that looks like this:
There are thirteen modules in the SQLPSX installation, you can see the listing on the SQLPSX website under documentation.
Now lets import one of the modules into our PowerShell Session. To do this we use the Import-Module cmdlet followed by the name of the module we want to import.
Import-Module SQLMaint
Next let's see what functions are available in this module. We can do this using the Get-Command cmdlet and passing it the -Module parameter with the name of the module we just imported.
Get-Command -Module SQLMaint
You should now get a result that looks like this:
The best thing to do next is to pick a function that you like and see what it does and what parameters it takes. Let's look at the Get-SqlData function using our buddy the Get-Help cmdlet
Get-Help Get-SqlData
This returns the name of the function, a synopsis, syntax and a description. Notice the last section, Remarks:
Look at that! You can get a more detailed description and even examples. This is pretty cool, right!
Play around by importing the other modules and checking out the functions. I like to use the SQLMaint and Agent Modules but check out the other ones to see what is available. If you find something cool, post it in the comments!
Don't Reinvent The Wheel
Like I said before you can connect to SQL just fine without using SQLPSX and do everything you can with it but you will end up writing more code. Let's take a look at two examples. The first example will connect to my server and return a result set from a query:Now lets do the same thing using SQLPSX:
Fourteen lines of code vs five lines of code. Pretty easy decision in my head. I have enough to do during the day without having to remember all that code just to run a query. If you used method one you would probably end up writing your own module and importing it in all your scripts but why bother when the resource is already available. I want to point out that SQLSPX isn't doing anything different, the functions that they give you use the same code that you can hand write, they have just already done all the leg work for you. If you want to see what is going on "behind the scenes" you can open up the SQLServer.psm1 file in your PowerShell modules directory in the SQLServer folder.
Links and References
Download SQLPSX from CodeplexLearn More About SMO and PowerShell
Sum Up
That's it for this week. This lesson is important because I am going to use the SQLPSX modules in all of my later examples. Download it and play around and check in next week and we will get started on putting this all together to help simplify your life as a DBAHasta Luego,
RutherfordTX
great job! The author giving the present post, this is very unique and worthy content. I am waiting for your another post...
ReplyDeleteOracle Training in Chennai
Oracle Training institute in chennai
Oracle DBA Training in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Tableau Training in Chennai
Social Media Marketing Courses in Chennai
Oracle Training in Chennai
Oracle Training institute in chennai
always nice when a spam bot gives you a complement. If an actual real human being gets to this post, check out http://dbatools.io, SQLPSX hasn't been worked on in years but DbaTools is an active project that is doing great things with PowerShell and SQL
ReplyDeleteYour post is really awesome .it is very helpful for me to develop my skills in a right way
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Bangalore
Software Testing Training in Coimbatore
Software Testing Training in Madurai
Software Testing Training Institute in Chennai
Software Testing Course in Chennai
Testing Course in Chennai
Software Testing Training Institute in Bangalore
Selenium Course in Bangalore
Thank you much more for giving the great post and I hope more from your blog.
ReplyDeleteLinux Course in Chennai
best linux training in chennai
Spark Training in Chennai
Appium Training in Chennai
Power BI Training in Chennai
JMeter Training in Chennai
Placement Training in Chennai
Soft Skills Training in Chennai
Linux Training in T Nagar
Linux Training in Velachery
The blog you shared is very good. I expect more information from you like this blog. Thankyou.
ReplyDeleteweb designing training in chennai
web designing training in bangalore
web design training coimbatore
Web Designing Course in bangalore
web designing course in madurai
Web development training in bangalore
Web development training in chennai
Big Data Course in Coimbatore
Amazing Post. keep update more information.
ReplyDeleteIELTS Coaching in Chennai
IELTS Coaching centre in Chennai
IELTS Online Coaching
IELTS Coaching in Coimbatore
IELTS coaching in Madurai
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
tiktok jeton hilesi
SAC EKİMİ ANTALYA
referans kimliği nedir
İnstagram Takipçi Satın Al
METİN2 PVP SERVERLER
İNSTAGRAM TAKİPCİ
smm panel
ReplyDeletesmm panel
İş ilanları blog
İnstagram Takipçi Satın Al
hirdavatciburada.com
Https://www.beyazesyateknikservisi.com.tr/
SERVİS
tiktok jeton hilesi
It is captivating to have such a down-loader director by way of this competence. It provides an all-inclusive slip-up recuperation utensil. IDM 6.41 Build 2 Crack
ReplyDeleteMay this Independence Day bring unity and prosperity to Pakistan. May the stories of bravery of our heroes be recalled and may we join hands to make our country green again. Happy Independence Day! We wish you all happy independence day. Independence Day Quotes Pakistan
ReplyDeleteGood content. You write beautiful things.
ReplyDeletemrbahis
hacklink
mrbahis
sportsbet
hacklink
taksi
korsan taksi
vbet
vbet
van
ReplyDeleteyalova
diyarbakır
düzce
gümüşhane
CVB
kıbrıs
ReplyDeleteedirne
muş
trabzon
balıkesir
ZB2TZ
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
ZUR