<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'PowerShell' and 'ssis'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=PowerShell,ssis&amp;orTags=0</link><description>Search results matching tags 'PowerShell' and 'ssis'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Export all SSIS packages from msdb using Powershell</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2011/02/02/export-all-ssis-packages-from-msdb-using-powershell.aspx</link><pubDate>Wed, 02 Feb 2011 07:45:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:33124</guid><dc:creator>jamiet</dc:creator><description>&lt;P&gt;Have you ever wanted to dump all the SSIS packages stored in msdb out to files? Of course you have, who wouldn’t? Right? Well, at least one person does because this was the subject of a thread (&lt;A href="http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/cd5f9ed6-8dd2-4ba7-9acd-b6ce2c04b7a1" target=_blank&gt;save all ssis packages to file&lt;/A&gt;) on the &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/threads" target=_blank&gt;SSIS forum&lt;/A&gt; earlier today.&lt;/P&gt;
&lt;P&gt;Some of you may have already figured out a way of doing this but for those that haven’t here is a nifty little script that will do it for you and it uses our favourite jack-of-all tools … Powershell!!&lt;/P&gt;
&lt;P&gt;Imagine I have the following package folder structure on my Integration Services server (i.e. in [msdb]):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/jamie_thomson/image_2014D2B2.png"&gt;&lt;IMG style="BACKGROUND-IMAGE:none;BORDER-RIGHT-WIDTH:0px;MARGIN:;PADDING-LEFT:0px;PADDING-RIGHT:0px;DISPLAY:inline;BORDER-TOP-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-LEFT-WIDTH:0px;PADDING-TOP:0px;" title=image border=0 alt=image src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_2BCA19E4.png" width=442 height=239&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;There are two packages in there called “20110111 Chaining Expression components” &amp;amp; “Package”, I want to export those two packages into a folder structure that mirrors that in [msdb]. Here is the Powershell script that will do that:&lt;/P&gt;&lt;PRE&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#0000ff;"&gt;Param&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$SQLInstance&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;localhost&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#000000;"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR:#008000;"&gt;#&lt;/SPAN&gt;&lt;SPAN style="COLOR:#008000;"&gt;####Add all the SQL goodies (including Invoke-Sqlcmd)#####&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#008000;"&gt;
&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;add-pssnapin&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;sqlserverprovidersnapin100&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-ErrorAction&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;SilentlyContinue&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;add-pssnapin&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;sqlservercmdletsnapin100&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-ErrorAction&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;SilentlyContinue&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;&lt;FONT size=1&gt;cls&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#000000;"&gt; 

&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$Packages&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;  &lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;Invoke-Sqlcmd&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-MaxCharLength&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;10000000&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-ServerInstance&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$SQLInstance&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-Query&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#800000;"&gt;WITH cte AS (
                                                                        SELECT    cast(foldername as varchar(max)) as folderpath, folderid
                                                                        FROM    msdb..sysssispackagefolders
                                                                        WHERE    parentfolderid = '00000000-0000-0000-0000-000000000000'
                                                                        UNION    ALL
                                                                        SELECT    cast(c.folderpath + '\' + f.foldername  as varchar(max)), f.folderid
                                                                        FROM    msdb..sysssispackagefolders f
                                                                        INNER    JOIN cte c        ON    c.folderid = f.parentfolderid
                                                                    )
                                                                    SELECT    c.folderpath,p.name,CAST(CAST(packagedata AS VARBINARY(MAX)) AS VARCHAR(MAX)) as pkg
                                                                    FROM    cte c
                                                                    INNER    JOIN msdb..sysssispackages p    ON    c.folderid = p.folderid
                                                                    WHERE    c.folderpath NOT LIKE 'Data Collector%'&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;

&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#0000ff;"&gt;Foreach&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$pkg&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#0000ff;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$Packages&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#000000;"&gt;)
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$pkgName&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$Pkg&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:#8b4513;"&gt;name&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;FONT size=1&gt;    &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#800080;"&gt;$folderPath&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$Pkg&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:#8b4513;"&gt;folderpath&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;FONT size=1&gt;    &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#800080;"&gt;$fullfolderPath&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;c:\temp\$folderPath\&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;FONT size=1&gt;    &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#0000ff;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:#ff0000;"&gt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;test-path&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-path&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$fullfolderPath&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#000000;"&gt;))
    {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;"&gt;mkdir&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800080;"&gt;$fullfolderPath&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; | &lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;Out-Null&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;FONT size=1&gt;    }
    &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT size=1&gt;&lt;SPAN style="COLOR:#800080;"&gt;$pkg&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:#8b4513;"&gt;pkg&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; | &lt;/SPAN&gt;&lt;SPAN style="COLOR:#5f9ea0;FONT-WEIGHT:bold;"&gt;Out-File&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-Force&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-encoding&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;ascii&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-STYLE:italic;COLOR:#5f9ea0;"&gt;-FilePath&lt;/SPAN&gt;&lt;SPAN style="COLOR:#000000;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;$fullfolderPath\$pkgName.dtsx&lt;/SPAN&gt;&lt;SPAN style="COLOR:#800000;"&gt;"&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR:#000000;"&gt;
&lt;FONT size=1&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;To run it simply change the “localhost” parameter of the server you want to connect to either by editing the script or passing it in when the script is executed. It will create the folder structure in C:\Temp (which you can also easily change if you so wish – just edit the script accordingly). Here’s the folder structure that it created for me:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/jamie_thomson/image_7578F1D4.png"&gt;&lt;IMG style="BACKGROUND-IMAGE:none;BORDER-RIGHT-WIDTH:0px;MARGIN:;PADDING-LEFT:0px;PADDING-RIGHT:0px;DISPLAY:inline;BORDER-TOP-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-LEFT-WIDTH:0px;PADDING-TOP:0px;" title=image border=0 alt=image src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_33FE3F7B.png" width=537 height=222&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Notice how it is a mirror of the folder structure in [msdb].&lt;/P&gt;
&lt;P&gt;Hope this is useful!&lt;/P&gt;
&lt;P&gt;&lt;A href="http://twitter.com/jamiet"&gt;@Jamiet&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;UPDATE: THis post prompted Chad Miller to write a post describing his Powershell add-in that utilises a SSIS API to do exporting of packages. Go take a read here: &lt;A href="http://sev17.com/2011/02/importing-and-exporting-ssis-packages-using-powershell/"&gt;http://sev17.com/2011/02/importing-and-exporting-ssis-packages-using-powershell/&lt;/A&gt;&lt;/P&gt;</description></item><item><title>PowerShell to Validate SAN File Migration</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2009/08/31/powershell-to-validate-san-migration.aspx</link><pubDate>Mon, 31 Aug 2009 19:03:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:16465</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;P&gt;This post is a bit off topic for SQL Server proper, but I thought someone out&amp;nbsp;'oogling teh inter-tubes might find it useful. Our group recently had to perform a large SAN migration both to new equipment and to a new geographic location. We started with a replication method provided by the SAN vendor. I won't mention who, but from where I sit I can't prevent you from guessing :-). We had to move a &lt;EM&gt;lot&lt;/EM&gt; of files; the great majority made the move intact but, perhaps because we are picky, we needed every single, individual file to be copied successfully. The files are just files, not SQL Server data, but they are important. It came to light that a few got ...&amp;nbsp;what's the technical term ... "whacked" in transit.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;File Copy Validation&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;After the initial copy, we needed a method to validate the files. Since the files are in two physical locations, directly comparing them is impractical, as it would take all the same bandwidth that it took to transmit the files in the first place. So,&amp;nbsp;we cooked up&amp;nbsp;a PowerShell script based on some examples that find duplicate files using a simple MD5 hash, and used the script to output hash values and full paths for each system into two sets of CSV files, each in one of the two locations.&lt;/P&gt;
&lt;P&gt;(If you haven't tried PowerShell yet, I have to say I am finding that in the grand scheme of things, the order of good stuff goes "Sliced Bread," then "PowerShell," a very close second.)&lt;/P&gt;
&lt;P&gt;From the resulting CSV files, a quick SSIS load into SQL Server tables and it was&amp;nbsp;possible to have SQL do the heavy lifting, finding any files where the full path is the same but the computed hashes don't match. From there,&amp;nbsp;thirty seconds of additional effort made XCOPY commands to re-write the corrupt files from the source system to the destination. We were seaching for relatively few files out of millions, so this was a huge time saver.&lt;/P&gt;
&lt;P&gt;This is just a quick and dirty administrative script, so I'm afraid it's not polished code, lacking niceties like error handling or parameters. YMMV, &amp;amp;c.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
#-------------------------------------------------#
# This script will read all files in a directory and subdirectories
# and compute a hash value for the content of each file, storing the
# hash in a text file report. Use to binary-compare sets of files in
# two locations.
#
# Created 8/27/2009 by Merrill Aldrich using logic from this
# duplicate file scanning method: 
# http://blogs.msdn.com/powershell/archive/2006/04/25/583225.aspx
#-------------------------------------------------#

# Set directory to scan and output file to store hash values

$searchFolder = "E:\SomeDirectory"
$outFile = "C:\File_Name_Of_your_choice.csv"

$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider]
$hashAlgorithm = new-object $cryptoServiceProvider

# Header for CSV format output file:
"File,CreateDate,ModDate,Hash" | Out-File $outFile 

# Scan each target file, recursively walk subfolders
"Scanning"
[int]$i = 0 # counter, just for user feedback

Get-Childitem -recurse $searchFolder `
	| Where-Object { $_.PSIsContainer -eq $false } `
	| foreach {   
	
		# Show progress every 'x' files
		$i += 1
		if ( $i%100 -eq 0 ) { $i.toString() + " Files Processed" | out-host }

		# compute a hash value for file content
		$stream = $null
		$hashByteArray = $null
		
		$stream = $_.OpenRead();
		$hashByteArray = $hashAlgorithm.ComputeHash($stream);
		$stream.Close();
	    
		# close the file stream in case of error
		trap {
			$_.Exception.Message | out-host
			if ($stream -ne $null) { $stream.Close(); }
			break;
		}

		$currentHash = [string]$hashByteArray

		# append file name, dates, hash to the output text file
		"""" + $_.fullname + """," `
			+ $_.creationtime.ToString( "s" ) + "," `
			+ $_.lastwritetime.ToString( "s" ) + "," `
			+ $currentHash `
			| Out-File -append $outFile
		
	}
$i.toString() + " Files Processed"
"Scan Complete."
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This process has worked well for us. The only speed bump: the standard format for writing date stamps in sortable format, &lt;FONT face="Courier New"&gt;$_.creationtime.ToString( "s" )&lt;/FONT&gt;, makes strings like &lt;FONT face="Courier New"&gt;2004-05-03T14:07:32&lt;/FONT&gt;, which are perfectly good ISO dates as fas as I know, but SSIS's standard import-wizard-generated packages seem to choke on the "T." To work around that I had to add a little Derived Column / Replace expression into the import packages to remove the "T" character from the date stamps. It's so like SSIS, which I generally like very much, to go 95% of the way but make you work for that last 5%...&lt;/P&gt;</description></item></channel></rss>