sp_BlitzIndex™ ouput to Excel with Powershell

I am impressed with the output from sp_BlitzIndex™ and today I tried to save it to an excel file so that I could pass it on to the developer of the service. When I opened it in Excel and imported it from the csv file it didn’t keep the T-SQL in one column due the commas which bothered me so I decided to use Powershell to output the format to Excel as follows

#############################################################################################
#
# NAME: SPBlitzIndexToCSV.ps1
# AUTHOR: Rob Sewell https://newsqldbawiththebeard.wordpress.com
# DATE:22/06/2013
#
# COMMENTS: This script will take the output from spBlitzIndex™ and
# export it to csv without splitting the tsql commands
# ————————————————————————

$Server = Read-Host “Please enter Server”
$Database = Read-Host “Enter Database Name to run spBlitzIndex against”
$filePath = “C:\temp\BlitzIndexResults”
$Date = Get-Date -format ddMMYYYY
$FileName = “Blitzindex_” + $Database + “_” + $Date + “.csv”$Query = “EXEC dbo.sp_BlitzIndex @database_name=’$Database’;”
$Blitz = Invoke-SQLCMD -server $Server -database master -query $Query$Blitz|Export-Csv $FilePath\$FileName

Please don’t ever trust anything you read on the internet and certainly don’t implement it on production servers without first both understanding what it will do and testing it thoroughly. This solution worked for me in my environment I hope it is of use to you in yours but I know nothing about your environment and you know little about mine

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s