South Florida IT Camp Saturday 2011 on July 23rd

We are pleased to announce the first South Florida IT Camp on Saturday, July 23rd 2011 from 8:00 am to 5:00 pm at Nova Southeastern in Fort Lauderdale.

Parking and admittance to this event is free.  Lunch will provided. Please Register now. Let your friends and colleagues know about this event.

Join system administrators IT professionals and database professionals in addition to managers at all levels in Florida that work with Microsoft technologies for a “IT Camp Saturday”. IT Camps are a free, one day learning event for anyone seeking professional development. This event serves IT professionals and students with a focus on IT Pro related technologies. IT Camps offer a conference style learning environment free of charge to attendees and is open to presenters of
all backgrounds and expertise.

Have questions? Comment or Contact Us.

Call for Speakers is Open

The call for speakers is open until 6/11. Topics can be anything applicable to System admins, and/or IT Pros including SharePoint, System Center, SQL Server, PowerShell, Active Directory, or Windows 2008 R2 and Windows 7.

To submit your session, please Click Here, or if you have questions? Comment or contact us.

Get-dbTableColumns function submitted to the Script Gallery

Based on recent article about getting SQL Server table columns information, I decided to create a function “Get-dbTableColumns.ps1” and posted into the Microsoft Scritp Gallery. I use some of our favorite community tools from Chad Miller and Pinal Dave to bring in a practical and useful command.

So, checked it out at, “Get database table columns list”: http://gallery.technet.microsoft.com/scriptcenter/21c90399-d408-428d-92a5-902e037fdd9b

Instructions are included.

Hint: Want to save result to a text file? Try…

Get-dbTableColumns -DatabaseName YourDbName | Out-File -FilePath c:\TableInfo.txt

Enjoy!

SQL PowerShell – List your DB Table columns information…

Making some addition to Pinal Dave T-SQL code provided in one of his blog on “List All The Column With Specific Data Types” we can use PowerShell to list our database table’s columns in an efficient way.  But, for the information what I needed to produce I had to add a few things to Pinal’s code.

Keep in mind, PowerShell can assist the DBA in effectively providing better T-SQL results.  Let me show you how we can leverage this T-SQL code with PowerShell.

Here’s the original SQL script executed in SSMS and the actual results shown in the following picture:

(Pinal Dave SQL Script)


This is a start but I needed a little more information, and a better way to display it.

First, I added two other ‘joins’ to the SQL script to include the ‘sys.tables’ and ‘sys.schemas’. Then, I customized the script a little more to get the results I wanted and ran it on my SSMS:

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;


As you can see, now I included Schema with Table names and removed some columns. Now, let’s put PowerShell in action. I’m going to use my modified T-SQL script, create a simple PowerShell script and run it under SQL PowerShell.

Prerequisite for executing this script, you need to have:

  1. SQL Server 2008 (or above)
    1. Download Chad Miller SQLPS module for SQL Server 2008 and 2008 R2
    2. Or, Download my version of SQLPSv2 for SQL Server ‘Denali’.
  2. PowerShell 2.0

Using the any of the SQLPS modules, you can create a simple PowerShell script to generate our table columns information with a few lines of code:

  1. Store the T-SQL script code in a string variable.
  2. Load the SQL PowerShell Module using the “Import-Module” command.
  3. Run the T-SQL query using the “Invoke-SQLCmd” to store the information in a variable.
  4. Then, we can manipulate the results we just saved in a variable.

Here’s the script:

$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;
@
Import-Module SQLPSv2
$SavedResults1 = Invoke-SQLCmdServerInstance
“MAX-PCWIN1”Database
“Adventureworks”Query
$sqlQuery
$SavedResults1 | ftauto

The last line of this script will list all content of your stored results and display it in a table format.


We got all tables listed one after another. So, we can add a few more lines of code to separate the tables:

## – First create a list of all the tables in the stored object ‘$SavedResults1’
$TableName = $SavedResults1 | selectunique SchemaTableName | Sort SchemaTableName
## – Verify all tables selected and in order
$TableName

Next step is to use a “ForEach-Object” command to help us break out the tables information individually, and display it on the PowerShell console:

foreach($t
in
$TableName){
    $SavedResults1 | where {$_.SchemaTableName -eq
$t.SchemaTableName} | `
    select SchemaTableName,ColumnName,SchemaName,TypeName,max_length | FTAuto
}


Now, you can be creative in how this information can be displayed, or even exported to another file format such as a ‘CSV’ or ‘TXT’ type. Just try it!!

Here’s the complete simple PowerShell script: 

 

 Stay tuned for more!  

To get script file, download here: 

 

 

Tips on scripting with PowerShell objects…

PowerShell is a great tool for collecting information. Here’s some direct tips to try to minimize the pain of coding, or at  least make it (kind of) readable.  I’ve seen some blogs out there that turn some people way just because their script is working.  But, that’s the beauty of PowerShell.  No matter how you build the code, if the end results is what you’re looking for, then good for you. 

I hope later on you realized that your script can be improved, and you will feel the POWER of POWERSHELL!!!

Briefly, I will be covering PSObject, Hash table, and a bonus Array objects

Check this out.  Here’s an example of some script lines building a PowerShell variable object from two Cmdlets executing two different WMI classes: Win32_OperatingSystem and Win32_BIOS.

Original Sample

   1: $ComputerName = "."

   2: $wmi = Get-WmiObject Win32_OperatingSystem –comp $ComputerName | Select CSName,BuildNumber,ServicePackMajorVersion;

   3: $obj = New-Object PSObject;

   4: $obj | Add-Member NoteProperty BuildNumber ($wmi.BuildNumber);

   5: $obj | Add-Member NoteProperty CSName ($wmi.CSName);

   6: $obj | Add-Member NoteProperty SPVersion ($wmi.ServicePackMajorVersion);

   7: $wmi = Get-WmiObject Win32_BIOS –comp $ComputerName | Select SerialNumber;

   8: $obj | Add-Member NoteProperty BIOSSerial ($wmi.SerialNumber);

   9: $obj;

Now, the above code will work.  In my opinion using Add-Member is already old school. 

image

At the end, I checked for the PS variable ($obj) type, using the “.gettype()” method.  This confirmed this variable a PSCustomObject.

I will show you other ways to get a similar results, and hopefully with less typing.  Maybe, it will look nicer too.  The goal is, as you progress using PowerShell for you day-to-day activity, you will continue to improve your existing code.

Improvement #1

   1: $ComputerName = "."

   2: $wmi1 = Get-WmiObject Win32_OperatingSystem –comp $ComputerName | Select CSName,BuildNumber,ServicePackMajorVersion;

   3: $wmi2 = Get-WmiObject Win32_BIOS –comp $ComputerName | select SerialNumber;

   4:  

   5: $results = $wmi1|select csname,BuildNumber,ServicePackMajorVersion,@{Label=”BIOSSerialNum”;Expression={$wmi2.SerialNumber}};

   6: $results;

As you can see, here I’m first collecting all the information by building my PowerShell variables in $wmi1 and $wmi2 which has different results.  Then, I’m combining the values I want from my $wmi1 and $wmi2 using the “Select” command.

image

So we made some improvements from originally 9 lines of code cut down to 5 lines.  And, notice that the new PS variable type still is “PSCustomObject”.

Now, let me show you another very nice variation that is intended to replace the use of “Add-Member” command.  We are going to use the Hash table concept to create our “PSCustomObject” variable.

Here’s another way which is better looking with less typing than our first example.

Improvement #2

   1: $wmi1 = Get-WMIobject -class Win32_OperatingSystem;

   2: $wmi2 = Get-WmiObject Win32_BIOS

   3:  

   4: $PCHashTable = @{

   5:     BuildNumber  = $wmi1.BuildNumber;

   6:     csname       = $wmi1.csname;

   7:     ServPackVersion = $wmi1.ServicePackMajorVersion;

   8:     BIOSSerialNum   = $wmi2.SerialNumber;

   9: }

  10:  

  11: $MyPSObject = New-Object PSObject -Property $PCHashTable;

  12: $MyPSObject;

image

As you can see, this PowerShell code have 10 lines but, for me, it looks cleaner and easy to read.  Basically, we are breaking the code in three sections:

1. Create the variable objects holding our results.

2. Build the hash table where we combine all our selected objects.

3. Convert the hash table to a “PSCustomObject”, so we can display the results in a proper format.  (see next pic.)

image

Hidden Tips:

Check in both improvement samples (1 & 2), I’m showing how you can create/change the label of your variable property in the “Select” command, and when you are creating a Hash table.

Bonus Script: Using Arrays objects to Collect information

Here’s a snippet you could use the gather information about your SQL Servers using .NET SMO SQL Enumeration to get all SQL names on the network.  To run this script you need to have SQLPS loaded in you PowerShell session and have proper server Windows Authentication.

I hope you’ll like it:

   1: ## For SQL Server need SQLPS to be loaded

   2: ##

   3: $SQLSvr = [Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers($false) | Select name;

   4:  

   5: [Array] $SQLinfo += ForEach($SQL in $SQLSvr){

   6:     $MySQL = new-object(“Microsoft.SqlServer.Management.Smo.Server”) $SQL.Name;

   7:     $MySQL | Select NetName, Product, EngineEdition, Version, Platform, ProductLevel;

   8: } 

   9:  

  10: $SQLinfo | select Netname, Product, EngineEdition, Version, ProductLevel | ft -auto;

Hidden Tip: Notice, I  have created an Array object ($SQLInfo) in line 5, in which the “ForEach()” command will be use to add the results from each element read from the server object ($SQLSvr).

BIG REMINDER!!!  Remember that your PS variables will probably have more values you can display in your results using the “.. |Select val1, val2, val5…”.  Use the Get-member command to exposed them.  (see next pic.)

image

That’s it for now!!

PrimalSQL and PrimalForms 2011, a perfect couple…

Here’s a quick overview of both PrimalSQL and PrimalForms 2011 working together.  These products gives you the ability to create GUI PowerShell scripts on the fly so you cut down you scripting time.

PrimalSQL2011

PrimalForms2011

I am test driving the upcoming new SAPIEN Technologies PrimalSQL 2011, and especially the new option to either “Generate Query Form…”, or “Generate Query PowerShell Script…”.   The best is the ability the generate a basic query form that you can change using the new upcoming PrimalForms 2011.  Both products are looking very solid with few bugs, and it’s still not in Beta yet!  It’s impressive.

Here’s both products working together:

PrimalSQLFORMS2011

Stay tuned for more information about this new product releases! 

Checkout more on exporting to a PowerShell script on SAPIEN Technologies blog: http://blog.sapien.com/index.php/2011/01/11/feature-peek-2011-primalsql-exporting-powershell-guis/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+SapienBlog+%28SAPIEN+Technologies+Blog%29

SQL Azure available PowerShell Cmdlets…

Here’s some links you check on available cmdlets for SQL Azure:

Windows Azure Service Management CmdLets: http://code.msdn.microsoft.com/azurecmdlets

Exercise 2: Using PowerShell to Manage Windows Azure Applications: http://msdn.microsoft.com/en-us/wazplatformtrainingcourse_deployingapplicationsinwindowsazurevs2010_topic3.aspx

This something to keep in mind.  Azure is here to stay!

SQL Denali missing “IF Exist…", default option is “False”…

On my previous blog, I mention about the missing “IF EXIST …” SQL Statement when scripting out a Drop/Create a table.

image

I should have known that there’s an option you can change this behavior.  On SSMS Denali, look under “Tools | Options | SQL Server Object Explorers | Scripting”, and under “Object Scripting Options” you will find the “Include IF NO EXIST Clause”.  Yes, you read it right!!  People have complain about the wording of this option.  Why not use “Include IF EXIST clause” or “Enable/Disable IF EXIST Clause”?   Well, as they say in Survivor…  It’s time to vote!

I found this Microsoft Connect feedback: http://connect.microsoft.com/SQLServer/feedback/details/624075/include-if-not-exists-clause-impacts-drop-scripting

Look like Microsoft is listening and they have a fix for the next release of Denali.  Good Job Microsoft! 

Can’t create SSIS solution in BIDS R2 after installing SQL Denali CTP1

Although, this is not PowerShell, I do work with SQL Server and SSIS packages.  I shouldn’t be surprise to find problems because I installed a CTP (Community Technology Preview).  I do expect to have some issue so I can figure out the answers, find the workaround to fix it, and then blog about it.

Well, here’s one issue I found after installing the new SQL Server ‘Denali’ CTP1 on my Windows 7 along with my RTM version of SQL Server 2008 R2.   I needed to develop a new SSIS solution using SQL Server 2008 R2 BIDS and I ended up with the following error:

image

image

image

So, my first thought was, Oh-Oh!! Machine rebuild! 

But, after having good experience with the recent release of SQL Server uninstall process, then I decided to do the following steps:  ( and it did work for me )

1. Under your Windows 7, Control Panel | Programs | Uninstall Programs, go to and right-click on “SQL Server Denali CTP1 Setup” or “SQL Server Denali CTP1 Setup (64bit)” and select “uninstall”.  You may need to media in order to proceed with the uninstall.

image

image

image

2. Go through the SQL Denali uninstall setup steps, until you reach to the “Select Instance” where you pick to “<<Remove shared features only>>”.

image

3. Now, you only going to “Select Feature” you really want to uninstall, which is “Business Intelligence Development Studio”.  Click Next to continue.

image

4. At the “Removal Rules” click Next, and at the “Ready to Remove”, Click Remove to start the uninstall process.

5. You will get the following screen, click Close to finish, and you will need to restart your machine.

image

image

Are we done yet?  No!! 

I want to make sure I uninstall all BIDS versions starting with SQL Server Denali and then SQL Server 2008 R2.  So, we need to repeat all previous steps but for SQL Server 2008 R2:

image

After, you’ve done repeating the steps and everything completed successfully, now we can install SQL Server 2008 R2 in order to recover this BIDS version.

image

image

Now, we are ready to re-install SQL Server 2008 R2 BIDS.  This step may ask you for the SQL Server media.

image

image

image

image

Keep clicking Next until the process starts installing the BIDS.

image

Now, the final test. Let’s open BIDS R2 and create an SSIS solution.

image

And, we got our BIDS R2 working again.

image

I hope this information helps everyone out there.

Smile

Denali – Get your SQLPSv2 module set to go…

Denali – PowerShell, Surprise! …not too fast. Beside the fact that PowerShell V2 is required before the installation, I was excited to see that thing were moving forward.  But wait a second,  still is a mini-shell,  and it still doesn’t run some of the nice PowerShell V2 cmdlet such as: Send-MailMessage or Out-GridView.  This is sad, you got these two useful cmdlets and you can’t use them unless  you customize your environments.  Well, there’s no escape from it.
Now, taking the lesson learned from Chad Miller’s blog on how to create your SQLPS module (http://sev17.com/2010/07/making-a-sqlps-module/), you can do the same thing with Denali and create a SQLPSV2 module so we can the new cmdlets the CTP1 have included.  Please, go ahead and grab the SQLPS module from his Chad’s blog in order to proceed with the following instructions.
Here’s how we create our new SQLPSv2 module?
1. I locate the Denali SQLPS component, which is in the following path: “C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn”
2. Under your “Documents\WindowsPowerShell\Module” folder, create the “SQLPSv2” folder.
3.  From the “..\Binn” folder we are going to copy the following things:
a. Copy/Paste the “\en” folder to the “SQLPSv2” folder”.
b. Copy/Paste these 2 *dll’s files: “Microsoft.SqlServer.Management.PSProvider.dll” & “Microsoft.SqlServer.Management.PSSnapin.dll”.
c. Copy/Paste these 2 *ps1xml files: “SQLProvider.Format.ps1xml” & “SQLProvider.Types.ps1xml”.
4. To complete our new SQLPSv2 module, we are going to copy the from Chad’s solution, the following two files but renaming it to SQLPSv2.*: Sqlps.psd1 –> SqlpsV2.psd1 and Sqlps.ps1 –> SqlpsV2.ps1.
At the end, you will have you Module folder looking like the following sample:
Directory: C:\Users\User01\Documents\WindowsPowerShell\Modules\SQLPSV2
Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-        11/10/2010   9:59 PM            en
-a—         9/25/2010   8:32 PM      92000 Microsoft.SqlServer.Management.PSProvider.dll
-a—         9/25/2010   8:32 PM      83808 Microsoft.SqlServer.Management.PSSnapins.dll
-a—         9/21/2010   6:14 PM      84619 SQLProvider.Format.ps1xml
-a—         9/21/2010   6:14 PM      15552 SQLProvider.Types.ps1xml
-a—          7/8/2010   4:38 PM        374 SqlpsV2.ps1
-a—        11/10/2010  10:05 PM       1062 SqlpsV2.psd1
Now, let us test if this works, by opening your PowerShell Console or ISE editor, then typing “Import-Module SQLPSv2 –DisableNameChecking
PS C:\>
PS C:\> Import-module SQLPSv2 -DisableNameChecking
PS C:\>
PS C:\> GCM *sql* -CommandType Cmdlet | Select name
Name
—-
Add-SqlAvailabilityGroupDatabase
Decode-SqlName
Encode-SqlName
Invoke-Sqlcmd
Join-SqlAvailabilityGroup
New-SqlAvailabilityGroup
New-SqlAvailabilityReplica
Remove-SqlAvailabilityGroup
Resume-SqlAvailabilityGroupDatabase
Suspend-SqlAvailabilityGroupDatabase
Switch-SqlAvailabilityGroup

PS C:\> cd SQLServer:\SQL\MAX-PCWIN1\MSQLDENALI01\Databases
PS SQLSERVER:\SQL\MAX-PCWIN1\MSQLDENALI01\Databases>
If you want to download my solution, just click the link below:

Stay tuned for more on SQL Server Denali CTP1 and SQL PowerShell.
Happy PowerShelling!!