2017-05-27 20 views
4

我已經建立了TDE加密的數據庫。現在我需要通過PowerShell禁用此加密。我能夠獲得一些突破,但面臨以下錯誤如何通過PowerShell關閉數據庫加密

錯誤:無法刪除數據庫加密密鑰,因爲它當前正在使用中。需要關閉數據庫加密才能刪除數據庫加密密鑰。然而,加密密鑰會關閉,但我相信密鑰會丟失。 下面是截圖它的外觀的代碼第一次運行之後

enter image description here

Below is the code that I have written/used: 

    function set-EncryptionOff($ExistingDB) 
{ 
    $ExistingDB.EncryptionEnabled=$false 
    $ExistingDB.Alter(); 
    $ExistingDB.DatabaseEncryptionKey.Refresh() 
    $ExistingDB.DatabaseEncryptionKey.Drop() 

} 

回答

3

你非常接近。將EncryptionEnabled設置爲false後,您需要執行$ExistingDB.Alter()以告知服務器實際執行此操作。一旦你這樣做了,你可以使用你已有的命令安全地刪除數據庫加密密鑰。

完整的腳本:

$sqlServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $sqlName 
$ExistingDB=$sqlServer.Databases.Item($dbname) 
$ExistingDB.EncryptionEnabled=$false 
$ExistingDB.Alter() 
$ExistingDB.DatabaseEncryptionKey.Refresh() 
$ExistingDB.DatabaseEncryptionKey.Drop() #should work now 
+0

嗨,本,感謝您的輸入..代碼現在正常工作+ 1 – sanketh

+0

是否有任何事件監聽器來識別數據庫解密的完成,因爲數據庫非常龐大,並且一旦解密完成後需要自動備份 – sanketh

+0

看起來您在此處詢問這是一個實際問題:https://stackoverflow.com/questions/44225996/event-handler-for-tde-decryption-process-in-powershell/44229087。在那裏提供答案。 –

2

看起來你可以使用Azure的PowerShell命令Set-AzureRMSqlDatabaseTransparentDataEncryption做到這一點:

Enabling and Disabling TDE on SQL Database by Using PowerShell

Using the Azure PowerShell you can run the following command to turn TDE on/off. You must connect your account to the PS window before running the command. Customize the example to use your values for the ServerName, ResourceGroupName, and DatabaseName parameters. For additional information about PowerShell, see How to install and configure Azure PowerShell .

..

To disable TDE:

Set-AzureRMSqlDatabaseTransparentDataEncryption -ServerName "myserver" -ResourceGroupName "Default-SQL-WestUS" -DatabaseName 

"database1" -State "Disabled"

If using version 0.9.8 use the Set-AzureSqlDatabaseTransparentDataEncryption command.

來源:https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/transparent-data-encryption-with-azure-sql-database

+0

集-AzureRMSqlDatabaseTransparentDataEncryption沒有一個公認的cmdlet時,可以請你告訴我,要包含哪些包? – sanketh

+0

我相信它是AzureRM模塊的一部分https://docs.microsoft.com/en-gb/powershell/azure/install-azurerm-ps?view=azurermps-4.0.0 –