(Updated: 07/09/2011 – Microsoft decided to remove the link to this Release Notes.)
From me (@MaxTrinidad): #SQLDenali #PowerShell – Make sure to read the release notes on PowerShell stuff: 1.1, 1.5, 2.4, 5.8, 5.9, 5.11.1, 5.11.3, & 8.1. Awesome!!
From me (@MaxTrinidad): #SQLDenali CTP3 comes with the long awaited SQLPS module… HURRAY!!
About the SQLPS Module. Finally, Microsoft have provided us with a PowerShell Module that will load all available SQL Server PowerShell commands (SQLPS). But, keep in mind, that this module only load the command use with the SQL Database Engine. You will still need to manually load the Master Data Services commands, plus whatever they will give us.
So, for now, it’s a waiting game to see SQL Server Denali CTP3 come out.
Here’s a good example of how useful is SMO in your SQL Server Developement environment. As I the task repopulating data in a quarterly basis, I need to clear all my staging tables in databases before my import processes. OK! I could just create a T-SQL query with all my truncate lines. But, this will force me to open SSMS, file open my T-SQL file, and then execute the query. Or, I can create simple PowerShell function so I can run from my PowerShell Console, or ISE editor when it’s needed.
In my previous blog, I showed the Get-TSQLTableRecordCount function which allow me to either list all my databases, or display a list of row count for all the tables in the database. I can use this function to verify that my PowerShell table truncate function is working.
So, when using SMO, how many lines of code it will take me to create this function? About 11 line of code. But truely, the heart of this code is only 3 lines, and rest if only adding the necessary code to it a function.
Here’s the basic 3 lines that make the truncate work:
Of course, you need to provide both the SQL Server instance, and the database name
Note: Be careful with this function because is meant to truncate all tables in the given Database.
I went back to SSMS to graphically confirm that all data was removed, but I found out that the database size was still showing. I knew I had to incorporate the SMO Shrink() method for the database after doing the SMO TruncateData() method on each of the tables.
Here’s an image showing the database size after the truncating all tables:
So, I added the following line after the truncate tables:
This is just a teaser code so you can see the potential and benefit in understanding SMO in PowerShell. I will be incorporating a more elaborate version of this function into my SQLDevTools Module available in CodePlex in the next release.
This script hold a total of 4 functions to help setup your missing PowerShell User profiles which are not created when installing Window Management Framework, nor setup during then new Windows 7/2008R2 Operating Systems.
I created the function “Get-dbTableRecordCount” to get both the tables record and data space used from a selected table. A the same time, if you can’t remember the databasename, then you can use the parameter ” -ShowAllDatabases Y ” to only list all databases from the provided SQL Server Instance name.
1. Must have either SQL Server 2008 (or above) installed, or the have the free version of SQL Server Management Studio 2008 R2 (no SQL Engine) installed.
2. And, to download either, or both of the SQLPS modules. Instructions are provided with the description of the function in the MS Script Gallery.
This functions will have the following 3 parameters:
1. ” -SQLInstanceName “ – This is mandatory entry. Enter your SQL Server instance name.
2. ” -DatabaseName “ – Optional but required with for the “-SQLInstanceName” parameter.
3. ” -ShowAllDatabses “ – Optional but only requiered with the “-SQLInstanceName” parameter and not with “-DatabaseName”.
An Alias name is provided: “gtrc”
Also, remember you can use “help”, or “get-help” commands to read documentation about this function.
Sample pics: (show databases only)
Application: You can save the results adn generate different out results.
I just posted a new PowerShell function to help get information about your SQL Server(s). Following Don Jones recent post about “Inventory SQL Server Versions on Multiple Computers” this is just to show you, if you have SQL Server 2008 (R2 or above), then you can use SMO which is already installed with SQL.
Please, make sure to meet the requirements to use this function. More information in the Script Gallery post.
Now, this new function include three cmdlet parameters that will affect your results.
” -SQLServerOption ” – This parameter will allow you to: 1) Individually get version information about one, or a series of SQL Server, or 2) using the ” -SQLServerOption all ” parameter, will get you a list of all SQL Servers on the network.
” -DateTimeStamped ” – *This parameter only accepts Y, any other character(s) will be ignored. This will results in writting the start and end date/time for running this command.
” -HideBlank ” – This parameter only accepts Y, any other character(s) will be ignored. This will ignored all blanks items selected with the ” -SQLServerOption ALL” because you may not have permission to access the SQL Instance.
By the way, I automatically included some SQL Server counter to let you know how many server(s) it found and the how many are listed in the results.
*Note: I included the Date/Time stamped is because this function will take some time to complete.
Sample picture:
Practical Use: – The following line will create a text file with your information:
PS> gmsv -SQLServerOption ALL | Out-File c:\MyFolder\MySQLVersionInfo.txt
Also, you can save the results to an object, then create a csv file. Here’s the starting point for your CSV:
Go ahead, and complete the task… I’m Just Saying!!
I love the PowerShell collaboration in the IT community. One of my twitter followers reminded me about the “Format-Table” command that allows you to group your collection using the parameter “-GroupBy” which work beautifully. There’s no need to add the “ForEach” command. I trully forgot to the check for this parameter.
This change in my script will saves be a few more lines of code.and here’s the final result. Here’s the script:
[sourcecode language=”powershell” 1=”wraplines"false"”]
## – Stored T-SQL modified script into a string variable
$sqlQuery = @"
SELECT
s.name+’.’+OBJECT_NAME(c.OBJECT_ID) as SchemaTableName
,c.name AS ColumnName
,SCHEMA_NAME(t.schema_id) AS SchemaName
,t.name AS TypeName
,c.max_length
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
JOIN sys.tables as t2 on t2.object_id = c.object_id
JOIN sys.schemas as s on s.schema_id = t2.schema_id
ORDER BY c.OBJECT_ID;
"@
## – Load the SQLPS module for Denali ( for 2008/200R2 is SQLPS)
Import-Module SQLPSv2
$SavedResults1 = Invoke-SQLCmd -ServerInstance "ISO-DESKTOP-65" -Database "ReportServer" -Query $sqlQuery
$SavedResults1 | ft -auto -GroupBy SchemaTableName
[/sourcecode]
And, here’s the sample PowerShell Console result:
This is a good example of how PowerShell can keep simplifying your script as yiou continue to grow.
Especial THANKS to David Moravec (http://www.powershell.cz) for tweet to giving me the tip to enhance this script. Now, you can download both versions:
OK! Calling all Florida West and Central Coast IT professionals. The first Florida ITCampSaturday is coming to Tampa, Florida on Saturday March 19th. It’s an all day free event you don’t want to miss.
Topics can be anything applicable to SysAdmins or IT Pros including: SharePoint, System Center, SQL Server, PowerShell, Active Directory, or Windows 2008 R2 and Windows 7.