Use PowerShell to encrypt SQL Server Stored Procedures

Source: Internet
Author: User

The following code uses a PowerShell script to encrypt all stored procedures in SQL Server.

For security reasons, we use with encryption to encrypt the stored procedure. But what should we do if we want to update the encrypted stored procedure? I have seen some people write a C # console program. I am prompted to use PowerShell to call 'Microsoft. SqlServer. Management. Smo.

First, open PowerShell. You need to determine whether your SQL Server version supports PowerShell. Here I use SQL Server 2008.

Open 'Microsoft SQL Server Management Studio '-> open 'object Explorer'-> <name of the database you want to process>, right-click it, and open 'start powershell'. A PowerShell Prompt window is displayed.

The modification code is as follows:

$ Srv = new-Object Microsoft. SqlServer. Management. Smo. Server ("[DataBase Engine Name]")
$ Db = New-Object Microsoft. SqlServer. Management. Smo. Database
$ Db = $ srv. Databases. Item ("[DataBase Name]")
Foreach ($ sp in $ db. StoredProcedures ){
If (! $ Sp. IsSystemObject ){
If (! $ Sp. IsEncrypted ){
$ Sp. TextMode = $ false;
$ Sp. IsEncrypted = $ true;
$ Sp. TextMode = $ true;
Try {
$ Sp. Alter ();
} Catch {
Write-Host "$ sp. Name fail to encrypted ."
}
}
}
}

It depends on the number of stored procedures.

Original English, OSCHINA Original translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.