使用 PowerShell 加密 SQL Server 的預存程序

來源:互聯網
上載者:User

下面代碼示範如何使用 PowerShell 指令碼將 SQL Server 中的所有預存程序加密。

出於安全的考慮,我們使用 WITH ENCRYPTION 在建立預存程序的時候進行加密,但如果我們想要更新已加密的預存程序該如何做呢?我看到一些人寫了一個 C# 控制台程式,受此提示,我使用了 PowerShell 調用 'Microsoft.SqlServer.Management.Smo' 來實現。

首先我們開啟 PowerShell,你需要確定的是你的 SQL Server 版本是否支援 PowerShell,我這裡用的是 SQL Server 2008.

開啟 'Microsoft SQL Server Management Studio' -> open 'Object Explorer'-> <你要處理資料庫名> ,然後右鍵開啟 'Start PowerShell', 將會彈出 PowerShell 提示視窗。

修改代碼如下所示:

$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."
        }
     }
  }
}

執行時請耐心,這取決於你的預存程序的數量。

英文原文,OSCHINA原創翻譯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.