4
4
5
5
[cmdletbinding ()]
6
6
param (
7
- [Parameter (Mandatory = $true , ValueFromPipeline = $true )] [string ] $ConnectionString ,
8
- [string ] $MasterKeyDNSName = " CN=Always Encrypted Sample Cert" ,
9
- [switch ] $RemoveExistingCerts ,
10
- [switch ] $ExportCertificate ,
11
- [switch ] $ExportCertificateKeys ,
12
- [string ] $MasterKeySQLName = " AlwaysEncryptedSampleCMK" ,
13
- [string ] $AuthColumnKeyName = " AuthColumnsKey" ,
14
- [string ] $AppColumnKeyName = " AppColumnsKey" ,
15
- [string ] $LogColumnKeyName = " LogColumnsKey"
7
+ [Parameter (Mandatory = $true , ValueFromPipeline = $true )] [string ] $ConnectionString ,
8
+ [string ] $MasterKeyDNSName = " CN=Always Encrypted Sample Cert" ,
9
+ [switch ] $RemoveExistingCerts ,
10
+ [switch ] $ExportCertificate ,
11
+ [switch ] $ExportCertificateKeys ,
12
+ [string ] $MasterKeySQLName = " AlwaysEncryptedSampleCMK" ,
13
+ [string ] $AuthColumnKeyName = " AuthColumnsKey" ,
14
+ [string ] $AppColumnKeyName = " AppColumnsKey" ,
15
+ [string ] $LogColumnKeyName = " LogColumnsKey"
16
16
)
17
17
18
- Import-Module SqlServer
19
-
20
18
try {
21
- $smoDatabase = Get-SqlDatabase - ConnectionString $ConnectionString
19
+ $smoDatabase = Get-SqlDatabase - ConnectionString $ConnectionString
22
20
}
23
21
catch {
24
- Write-Error $_
25
- break
22
+ Write-Error $_
23
+ break
26
24
}
27
25
28
26
if ($RemoveExistingCerts ) {
29
- Write-Verbose " Removing All Existing Certificates Named $ ( $MasterKeyDNSName ) "
27
+ Write-Verbose " Removing All Existing Certificates Named $ ( $MasterKeyDNSName ) "
30
28
$existingColumns = Get-SqlColumnEncryptionKey - InputObject $smoDatabase
31
29
$existingColumns | ForEach-Object {
32
30
Remove-SqlColumnEncryptionKey - Name $_.Name - InputObject $smoDatabase
33
31
}
34
32
Remove-SqlColumnMasterKey - Name $MasterKeySQLName - InputObject $smoDatabase
35
- Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq $MasterKeyDNSName | Remove-Item
33
+ Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq $MasterKeyDNSName | Remove-Item
36
34
}
37
35
38
- $Cert = (Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq ' CN=Always Encrypted Sample Cert' ) | Select-Object Thumbprint - First 1
36
+ $Cert = (Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq ' CN=Always Encrypted Sample Cert' ) | Select-Object Thumbprint - First 1
39
37
if ($Cert ) {
40
- Write-Verbose " Certificate `" $ ( $MasterKeyDNSName ) `" Already exists"
38
+ Write-Verbose " Certificate `" $ ( $MasterKeyDNSName ) `" Already exists"
41
39
}
42
40
else {
43
- Write-Host " Creating Self Signed Certificate `" $ ( $MasterKeyDNSName ) `" "
44
- $Cert = New-SelfSignedCertificate `
45
- - Subject $MasterKeyDNSName `
46
- - CertStoreLocation Cert:\CurrentUser\My `
47
- - KeyExportPolicy Exportable `
48
- - Type DocumentEncryptionCert `
49
- - KeyUsage DataEncipherment `
50
- - KeySpec KeyExchange
51
- = " CurrentUser/My/ $ ( $cert.ThumbPrint ) "
52
- Write-Verbose " Certificate Master Key Path: $ ( $cmkPath ) "
41
+ Write-Host " Creating Self Signed Certificate `" $ ( $MasterKeyDNSName ) `" "
42
+ $Cert = New-SelfSignedCertificate `
43
+ - Subject $MasterKeyDNSName `
44
+ - CertStoreLocation Cert:\CurrentUser\My `
45
+ - KeyExportPolicy Exportable `
46
+ - Type DocumentEncryptionCert `
47
+ - KeyUsage DataEncipherment `
48
+ - KeySpec KeyExchange
49
+ $CmkPath = " Cert:\ CurrentUser\My\ $ ( $cert.ThumbPrint ) "
50
+ Write-Verbose " Column Master Key Certificate Path: $ ( $CmkPath ) "
53
51
}
54
52
55
53
if ($ExportCertificate ) {
56
- Get-ChildItem Cert:\CurrentUser\My |
57
- Where-Object subject -eq " CN=Always Encrypted Sample Cert" |
58
- Export-Certificate - FilePath " $ ( $MasterKeySQLName ) .cer" | Out-Null
54
+ Get-ChildItem Cert:\CurrentUser\My |
55
+ Where-Object subject -eq " CN=Always Encrypted Sample Cert" |
56
+ Export-Certificate - FilePath " $ ( $MasterKeySQLName ) .cer" | Out-Null
59
57
}
60
58
61
59
if ($ExportCertificateKeys ) {
62
- Get-ChildItem Cert:\CurrentUser\My |
63
- Where-Object subject -eq " CN=Always Encrypted Sample Cert" |
64
- Export-PfxCertificate - FilePath " $ ( $MasterKeySQLName ) .pfx" - Password (ConvertTo-SecureString - String " 1234" - Force - AsPlainText) | Out-Null
60
+ Get-ChildItem Cert:\CurrentUser\My |
61
+ Where-Object subject -eq " CN=Always Encrypted Sample Cert" |
62
+ Export-PfxCertificate - FilePath " $ ( $MasterKeySQLName ) .pfx" - Password (ConvertTo-SecureString - String " 1234" - Force - AsPlainText) | Out-Null
65
63
}
66
64
67
- if ($smoDatabase.ColumnMasterKeys [' AlwaysEncryptedSampleCMK' ]) {
68
- Write-Warning " Master Key Reference $ ( $MasterKeySQLName ) already exists in the database."
65
+ if ($smoDatabase.ColumnMasterKeys [' AlwaysEncryptedSampleCMK' ]) {
66
+ Write-Warning " Master Key Reference $ ( $MasterKeySQLName ) already exists in the database."
69
67
}
70
68
else {
71
- # Create a SqlColumnMasterKeySettings object for your column master key.
72
- $cmkSettings = New-SqlCertificateStoreColumnMasterKeySettings `
73
- - CertificateStoreLocation " CurrentUser" `
74
- - Thumbprint $Cert.Thumbprint
75
-
76
- New-SqlColumnMasterKey - Name $MasterKeySQLName - InputObject $smoDatabase - ColumnMasterKeySettings $cmkSettings | Out-Null
69
+ # Create a SqlColumnMasterKeySettings object for your column master key.
70
+ $cmkSettings = New-SqlCertificateStoreColumnMasterKeySettings `
71
+ - CertificateStoreLocation " CurrentUser" `
72
+ - Thumbprint $Cert.Thumbprint
73
+
74
+ New-SqlColumnMasterKey - Name $MasterKeySQLName - InputObject $smoDatabase - ColumnMasterKeySettings $cmkSettings | Out-Null
77
75
}
78
76
79
- $ExistingColumnKeys = $smoDatabase.ColumnEncryptionKeys
77
+ $ExistingColumnKeys = $smoDatabase.ColumnEncryptionKeys
80
78
@ ($AuthColumnKeyName , $AppColumnKeyName , $LogColumnKeyName ) | ForEach-Object {
81
- if ($ExistingColumnKeys [$_ ]) {
82
- Write-Warning " Column Encryption Key already $_ exists."
83
- }
84
- else {
85
- $smoDatabase | New-SqlColumnEncryptionKey `
86
- - ColumnMasterKey $MasterKeySQLName `
87
- - Name $_ | Out-Null
88
- }
79
+ if ($ExistingColumnKeys [$_ ]) {
80
+ Write-Warning " Column Encryption Key already $_ exists."
81
+ }
82
+ else {
83
+ $smoDatabase | New-SqlColumnEncryptionKey `
84
+ - ColumnMasterKey $MasterKeySQLName `
85
+ - Name $_ | Out-Null
86
+ }
89
87
}
90
-
0 commit comments