適用於:
SQL Server
Azure SQL Database
Azure SQL 受控執行個體
SqlServer PowerShell 模組提供 Cmdlet 讓您在 Azure SQL Database 與 SQL Server 中設定
Always Encrypted
。
因為永遠加密的主要目標是為了確保已加密的敏感性資料安全無虞,即使資料庫系統遭到入侵亦然,所以在 SQL Server 電腦上執行處理金鑰或敏感性資料的 PowerShell 指令碼,可能會降低或損害此功能的優勢。 如需其他安全性相關建議,請參閱
金鑰管理的安全性考量
。
無論是否有角色隔離,您都可以使用 PowerShell 來管理 Always Encrypted 金鑰來控制能夠存取金鑰存放區中實際加密金鑰以及能夠存取資料庫的人員。
如需其他建議,請參閱
金鑰管理的安全性考量
。
將
SqlServer PowerShell 模組 22.0.50 版或更新版本
安裝在非裝載您SQL Server實例之電腦的安全電腦上。 此模組可以直接從 PowerShell 資源庫進行安裝。 如需更多詳細資料,請參閱
下載
指示。
匯入 SqlServer 模組
若要載入 SqlServer 模組:
使用
Set-ExecutionPolicy
Cmdlet,設定適當的指令碼執行原則。
使用
Import-Module
Cmdlet,匯入 SqlServer 模組。
此範例會載入 SqlServer 模組。
# Import the SQL Server Module.
Import-Module "SqlServer" -MinimumVersion 22.0.50
連線到資料庫
某些永遠加密 Cmdlet 會處理資料庫中的資料或中繼資料,因此要求您必須先連接到資料庫。 使用 SqlServer 模組設定永遠加密時,有兩種建議的方法可連接到資料庫:
使用 Get-SqlDatabase Cmdlet 進行連線。
使用 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 = " + $serverName + "; Database = " + $databaseName + "; Authentication = Active Directory Integrated"
$database = Get-SqlDatabase -ConnectionString $connStr
# 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 的永遠加密工作
使用 Provision 佈建 Always Encrypted 金鑰
使用 PowerShell 更換永遠加密金鑰
使用 PowerShell 利用 Always Encrypted 加密、重新加密或解密資料行
永遠加密 Cmdlet 參考
下列 PowerShell Cmdlet 可用於永遠加密:
CMDLET
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 之密碼編譯服務提供者 (CSP) 儲存在金鑰存放區中的非對稱金鑰。
Remove-SqlColumnEncryptionKey
從資料庫移除資料行加密金鑰物件。
Remove-SqlColumnEncryptionKeyValue
從資料庫中現有的資料行加密金鑰物件移除加密值。
Remove-SqlColumnMasterKey
從資料庫移除資料行主要金鑰物件。
Set-SqlColumnEncryption
加密、解密或重新加密資料庫中指定的資料行。
Always Encrypted 的金鑰管理概觀
使用 SQL Server Management Studio 設定 Always Encrypted
使用 Always Encrypted 開發應用程式