使用 PowerShell 設定永遠加密

適用於:SQL ServerAzure SQL 資料庫Azure SQL 受控執行個體

SqlServer PowerShell 模組提供 Cmdlet 讓您在 Azure SQL Database 與 SQL Server 中設定 Always Encrypted。

使用 PowerShell 配置 Always Encrypted 時的安全考量

因為永遠加密的主要目標是為了確保已加密的敏感性資料安全無虞,即使資料庫系統遭到入侵亦然,所以在 SQL Server 電腦上執行處理金鑰或敏感性資料的 PowerShell 指令碼,可能會降低或損害此功能的優勢。 如需更多安全性相關建議,請參閱 密鑰管理的安全性考慮。

無論是否有角色隔離,您都可以使用 PowerShell 來管理 Always Encrypted 金鑰來控制能夠存取金鑰存放區中實際加密金鑰以及能夠存取資料庫的人員。

如需更多建議,請參閱 密鑰管理的安全性考慮。

必要條件

將 SqlServer PowerShell 模組 22.0.50 版或更新版本安裝在未裝載 SQL Server 執行個體的安全電腦上。 此模組可以直接從 PowerShell 資源庫進行安裝。 如需更多詳細資料,請參閱下載指示。

注意

Microsoft 建議執行 Always Encrypted PowerShell 腳本時,使用 PowerShell 7 或更新版本。 PowerShell 7 提供更佳的跨平台支援、更佳的效能,以及與 SqlServer 模組(v22+)的最新相容性,這是許多 Always Encrypted 情境所必需的。

匯入 SqlServer 模組

若要載入 SqlServer 模組:

  1. 使用 Set-ExecutionPolicy Cmdlet,設定適當的指令碼執行原則。
  2. 使用 Import-Module Cmdlet,匯入 SqlServer 模組。

此範例會載入 SqlServer 模組。

# Import the SQL Server Module.  
Import-Module "SqlServer" -MinimumVersion 22.0.50

連線至資料庫

某些 Always Encrypted Cmdlet 會處理資料庫中的資料或中繼資料,因此需要您必須先連接到資料庫。 使用 SqlServer 模組設定永遠加密時,有兩種建議的方法可連接到資料庫:

  1. 使用 Get-SqlDatabase Cmdlet 進行連線。
  2. 使用 SQL Server PowerShell 提供者進行連線。

使用 Get-SqlDatabase

Get-SqlDatabase Cmdlet 可讓您連線至 SQL Server 或 Azure SQL Database 中的資料庫。 會傳回資料庫物件,然後使用連線至資料庫之 Cmdlet 的 InputObject 參數來傳遞。

使用 SQL Server PowerShell

# Import the SqlServer module
Import-Module "SqlServer" -MinimumVersion 22.0.50

# Connect to your database
# Set the valid server name, database name and authentication keywords in the connection string
$serverName = "<Azure SQL server name>.database.windows.net"
$databaseName = "<database name>"
$connStr = "Server=tcp:$serverName,1433;Database=$databaseName;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=Active Directory Interactive"

try {
	$database = Get-SqlDatabase -ConnectionString $connStr -Encrypt Mandatory -ErrorAction Stop
}
catch {
	Write-Error "Failed to connect. Verify server name, database name, Azure SQL firewall access, and Microsoft Entra permissions."
	throw
}

# List column master keys for the specified database.
Get-SqlColumnMasterKey -InputObject $database

或者,您可以使用管道:

$database | Get-SqlColumnMasterKey

使用 SQL Server PowerShell 提供者

SQL Server PowerShell 提供者會公開類似於檔案系統路徑之路徑中的 SQL Server 物件階層。 透過 SQL Server PowerShell,您可以使用 Windows PowerShell 別名來巡覽路徑,這與於一般用來巡覽檔案系統路徑的命令類似。 一旦您瀏覽至目標執行個體和資料庫後,後續的 Cmdlet 就會以該資料庫為目標,如下列範例所示。

注意

線上到資料庫的這個方法僅適用於 SQL Server(Azure SQL Database 不支援它)。

# Import the SqlServer module.
Import-Module "SqlServer" -MinimumVersion 22.0.50
# Navigate to the database in the remote instance.
cd SQLSERVER:\SQL\servercomputer\DEFAULT\Databases\yourdatabase
# List column master keys in the above database.
Get-SqlColumnMasterKey

或者,您可以使用泛型 Path 參數來指定資料庫路徑,而不是巡覽至資料庫。

# Import the SqlServer module.
Import-Module "SqlServer" -MinimumVersion 22.0.50
# List column master keys for the specified database.
Get-SqlColumnMasterKey -Path SQLSERVER:\SQL\servercomputer\DEFAULT\Databases\yourdatabase

使用 PowerShell 的任務總是加密

永遠加密 Cmdlet 參考

以下 PowerShell 命令可用於始終加密:

CMDLET 描述
Add-SqlAzureAuthenticationContext 對 Azure 執行驗證,並取得驗證權杖。
Add-SqlColumnEncryptionKeyValue(新增 SQL 欄位加密金鑰值) 為資料庫中現有的資料行加密金鑰物件新增加密值。
Complete-SqlColumnMasterKeyRotation 完成欄位主金鑰的輪替
Get-SqlColumnEncryptionKey 傳回資料庫中定義的所有資料行加密金鑰物件,或傳回具有指定名稱的一個資料行加密金鑰物件。
Get-SqlColumnMasterKey 傳回資料庫中定義的資料行主要金鑰物件,或傳回具有指定名稱的一個資料行主要金鑰物件。
Invoke-SqlColumnMasterKeyRotation 啟動欄位主金鑰的輪替。
New-SqlAzureKeyVaultColumnMasterKeySettings 建立 SqlColumnMasterKeySettings 物件,描述儲存在 Azure 金鑰保存庫中的非對稱金鑰。
New-SqlCngColumnMasterKeySettings 建立 SqlColumnMasterKeySettings 物件,描述儲存在支援新一代密碼編譯 (CNG) API 之金鑰存放區中的非對稱金鑰。
New-SqlColumnEncryptionKey 在資料庫中建立資料行加密金鑰物件。
New-SqlColumnEncryptionKeyEncryptedValue 產生資料行加密金鑰的加密值。
New-SqlColumnEncryptionSettings 建立 SqlColumnEncryptionSettings 物件,以封裝單一資料行加密的相關資訊,包括 CEK 和加密類型。
New-SqlColumnMasterKey 在資料庫中建立資料行主要金鑰物件。
New-SqlColumnMasterKeySettings 使用指定的提供者與金鑰路徑,為資料行主要金鑰建立 SqlColumnMasterKeySettings 物件。
New-SqlCspColumnMasterKeySettings 建立一個 SqlColumnMasterKeySettings 物件,用於描述儲存在金鑰存放區的非對稱金鑰,該金鑰受支持密碼編譯 API (CAPI) 的密碼編譯服務提供者 (CSP) 管理。
Remove-SqlColumnEncryptionKey 從資料庫移除資料行加密金鑰物件。
Remove-SqlColumnEncryptionKeyValue 從資料庫中現有的資料行加密金鑰物件移除加密值。
Remove-SqlColumnMasterKey 從資料庫中移除欄主索引鍵物件。
Set-SqlColumnEncryption 加密、解密或重新加密資料庫中指定的資料行。