Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Change Protection Level for all packages at once

$
0
0
Case

I created dozens of packages in my projectbut I forgot to change the default Protection Level in the project propertiesfrom "EncryptSensitiveWithUserKey" to "DontSaveSensitive". Now I have to change all packages one by one. Is there an alternative? I tried search and replace in the XML, but I can't find the Protection Level property.


Change Protection Level for all packages at once

Solution

Of course the best option is to prevent this from happening by setting the default before you start. You can do this in the properties of the project. All new packages will then inherit the Protection Level from the project.


Change Protection Level for all packages at once
Setting Protection Level on project

First, when trying to search and replace in the XML code of the packages you will notice that you cannot find the default 'EncryptSensitiveWithUserKey' which makes it hard to replace.


Change Protection Level for all packages at once
Default Protection Level is not in package

Secondly, the Protection Level is also stored in the Visual Studio project file (*.dtproj). When you open a package in design mode and press the save button it also updates metadata in the project file.


Change Protection Level for all packages at once
Protection Level in project file as well

Solution A

Good old Command Prompt to the rescue! The dtutil Utility can do the package conversion for you. If you are afraid of the Command Prompt or evennever heard about it, then don't use this solution.

1) Command Prompt

Open a Command Prompt and use CD ( Change Directory ) command to navigate to your folder with packages.


Change Protection Level for all packages at once
Navigate to your project folder with packages

2) Foreach Loop Container in DOS

Now you can call the dtutil Utility for each package in that folder with something similar as a Foreach Loop Container:

FOR %p IN (*.dtsx) DO dtutil.exe /file %p /encrypt file;%p;0 /quiet


Change Protection Level for all packages at once
The colors explain the command
3) Execute

When you execute the command, dtutil Utility will quickly change the Protection Level of all your packages.


Change Protection Level for all packages at once
101 packages changed within 5 seconds. Try that in Visual Studio!
4) Project Protection Level

If you haven't already done it, change the Protection Level in the Project Properties. See second screenshot of this blog post.


5) dtproj file

Now the project and all its packages have the same Protection Level, but the project doesn't now that yet. If you try to execute a package it will complain about the Protection Level inconsistencies.


Change Protection Level for all packages at once
Failed to execute the package or element. Build errors were encountered.

Error : Project consistency check failed. The following inconsistencies were detected:

MyPackage000.dtsx has a different ProtectionLevel than the project.

MyPackage001.dtsx has a different ProtectionLevel than the project.

To update the dtproj file you have to open all packages and then Re build the project. This will update the project file. Now you can execute the packages without the consistency error.


Change Protection Level for all packages at once
Open all packages and rebuild the project

Solution B

Good oldPowerShell to the rescue! This PowerShell script does the same as above, but also changes the project file. So no manual labour at all.Because the dtutil utility was so fast, I didn't edit the packages with .net libraries.It just executes dtutil in a hidden window.

The script is thoroughly tested for SSIS 2012-2016 from 'EncryptSensitiveWithUserKey' to 'DontSaveSensitive'. Other situations require more testing. Make sure to keep a copy of your project before using this script and let me know which situations require some more attention.


Change Protection Level for all packages at once
Change the protection level of the entire project in seconds #PowerShell script ################################ ########## PARAMETERS ########## ################################ $projectFolder = "C:\SSIS\myProject\myProject" $dtutilPath = "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\dtutil.exe" # The number changes per SQL Server version # 130=2016, 120=2014, 110=2012 # Also check the drive where SQL Server is # installed ################################################# ########## DO NOT EDIT BELOW THIS LINE ########## ################################################# clear Write-Host "=========================================================================================" Write-Host "== Used parameters ==" Write-Host "=========================================================================================" Write-Host "Project Folder :" $projectFolder Write-Host "dtutil Path :" $dtutilPath Write-Host "=========================================================================================" ###################################### ########## Check parameters ########## ###################################### # Test whether the paths are filled # and exists. if ($projectFolder -eq "") { Throw [System.Exception] "Project path parameter is mandatory" } elseif (-Not (Test-Path $projectFolder)) { Throw [System.IO.FileNotFoundException] "Project path $($projectFolder) doesn't exists!" } elseif (-Not $projectFolder.EndsWith("\")) { # Make sure path ends with \ for command $projectFolder = $projectFolder + "\" } if ($dtutilPath -eq "") { Throw [System.Exception] "dtutil parameter is mandatory" } elseif (-Not (Test-Path $dtutilPath)) { Throw [System.IO.FileNotFoundException] "dtutil not found at $($dtutilPath)" } ############################################# ########## dtutil for loop command ########## ############################################# # In this script we are executing dtutil.exe # Perhaps a bit quick & dirty, but more quick # than dirty. It changes 100 packages within # seconds. $command = "/C FOR %p IN ($($projectFolder)*.dtsx) DO dtutil.exe /file %p /encrypt file;%p;0 /quiet" Write-Host "Editing packages in $($projectFolder)... " -NoNewline # Open the command prompt (hidden) and execute # dtutil.exe with the parameters from above. Start-Process "C:\windows\System32\cmd.exe" -ArgumentList $command -Win

Viewing all articles
Browse latest Browse all 3160

Trending Articles