Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Package XML from SSIS Catalog with PowerShell

Is there a way to get the Package XML from the packages that are in the SSIS Catalog with PowerShell, but without downloading and extracting the project? I want to search for certain strings within the XML document.

################################
########## PARAMETERS ##########
################################ 
$SsisServer = ".\sql2016"



############################
########## SERVER ##########
############################
# Load the Integration Services Assembly
$SsisNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
[System.Reflection.Assembly]::LoadWithPartialName($SsisNamespace) | Out-Null;

# Create a connection to the server
$SqlConnectionstring = "Data Source=" + $SsisServer + ";Initial Catalog=master;Integrated Security=SSPI;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnectionstring

# Create the Integration Services object
$IntegrationServices = New-Object $SsisNamespace".IntegrationServices" $SqlConnection

# Check if connection succeeded
if (-not $IntegrationServices)
{
    Throw [System.Exception] "Failed to connect to server $SsisServer "
}



#############################
########## CATALOG ##########
#############################
# Create object for SSISDB Catalog
$Catalog = $IntegrationServices.Catalogs["SSISDB"]

# Check if the SSISDB Catalog exists
if (-not $Catalog)
{
    Throw [System.Exception] "SSISDB catalog doesn't exist!"
}



##########################
########## LOOP ##########
##########################
foreach ($Folder in $Catalog.Folders)
{
    Write-Host $Folder.Name
    foreach ($Project in $Folder.Projects)
    {
        Write-Host " - " $Project.Name
        foreach ($Package in $Project.Packages)
        {
            Write-Host "    - " $Package.Name
            # HOW TO GET PACKAGE XML???

            # Not working:
            # Exception calling "Serialize" with "1" argument(s):
            # "The parameter 'sink.Action' is invalid."
            #$sb = New-Object System.Text.StringBuilder
            #$sw = New-Object System.IO.StringWriter($sb)
            #$writer = New-Object System.Xml.XmlTextWriter($sw)
            #$xml = $Package.Serialize($writer)
        }
    }
}
like image 399
Joost Avatar asked Nov 05 '16 14:11

Joost


People also ask

How do I find the XML SSIS package?

To view the XML of your package right click the package and select View Code from within SSDT. This will open the XML for it. Also, any text editor, e.g. Notepad++, will do, too.

How do I download SSIS package from catalog?

Right-click on the SSIS Package file and open the visual studio, in my case I have Visual Studio 2017, open the project and right-click on the Package and paste your copied file here, now you can easily use this file as you need.

Where are SSIS catalog packages stored?

The default folder is the Packages folder, located in %Program Files%\Microsoft SQL Server\100\DTS. The MSDB folder lists the Integration Services packages that have been saved to the SQL Server msdb database on the server.


1 Answers

Albeit the thread being a few months old, I believe the below approach directly answers the original question. I encountered a similar situation whereby I needed to access the XML from a package in SSISDB to serve as a template for new packages. Well, after reading the project bytes into my script,

var projectBytes = ssisServer.Catalogs["SSISDB"].Folders[SSISFolderName].Projects[SSISProjectName].GetProjectBytes();

it was possible to bypass downloading/unzipping by creating an in-memory archive from the project bytes. By iterating through the archive entries until the correct package was found (easier if the index of the file is known), the last step was to read the archive entry into a stream and pass it to the package.LoadFromXML method.

Stream stream = new MemoryStream(projectBytes);

ZipArchive za = new ZipArchive(stream);

foreach (ZipArchiveEntry zipEntry in za.Entries)
{
    if (zipEntry.FullName == SSISPackageName)
    {
         Package pkg = new Package();

         StreamReader sr = new StreamReader(zipEntry.Open());

         pkg.LoadFromXML(sr.ReadToEnd(), null);

         break;

    }

}
like image 91
Jonathan Garvey Avatar answered Oct 22 '22 04:10

Jonathan Garvey