7
7
[Parameter (Mandatory = $true , ValueFromPipeline = $true )] [string ] $ConnectionString ,
8
8
[string ] $MasterKeyDNSName = " CN=Always Encrypted Sample Cert" ,
9
9
[switch ] $RemoveExistingCerts ,
10
+ [switch ] $ExportCertificate ,
11
+ [switch ] $ExportCertificateKeys ,
10
12
[string ] $MasterKeySQLName = " AlwaysEncryptedSampleCMK" ,
11
13
[string ] $AuthColumnKeyName = " AuthColumnsKey" ,
12
14
[string ] $AppColumnKeyName = " AppColumnsKey" ,
@@ -26,34 +28,63 @@ catch {
26
28
if ($RemoveExistingCerts ) {
27
29
Write-Verbose " Removing All Existing Certificates Named $ ( $MasterKeyDNSName ) "
28
30
$existingColumns = Get-SqlColumnEncryptionKey - InputObject $smoDatabase
29
- $existingColumns | % {
31
+ $existingColumns | ForEach-Object {
30
32
Remove-SqlColumnEncryptionKey - Name $_.Name - InputObject $smoDatabase
31
33
}
32
34
Remove-SqlColumnMasterKey - Name $MasterKeySQLName - InputObject $smoDatabase
33
35
Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq $MasterKeyDNSName | Remove-Item
34
36
}
35
37
36
- $cert = New-SelfSignedCertificate `
37
- - Subject $MasterKeyDNSName `
38
- - CertStoreLocation Cert:\CurrentUser\My `
39
- - KeyExportPolicy Exportable `
40
- - Type DocumentEncryptionCert `
41
- - KeyUsage DataEncipherment `
42
- - KeySpec KeyExchange
43
- $cmkPath = " CurrentUser/My/$ ( $cert.ThumbPrint ) "
44
- Write-Verbose " Certificate Master Key Path: $ ( $cmkPath ) "
45
-
46
- # Create a SqlColumnMasterKeySettings object for your column master key.
47
- $cmkSettings = New-SqlCertificateStoreColumnMasterKeySettings `
48
- - CertificateStoreLocation " CurrentUser" `
49
- - Thumbprint $cert.Thumbprint
50
-
51
- New-SqlColumnMasterKey - Name $MasterKeySQLName - InputObject $smoDatabase - ColumnMasterKeySettings $cmkSettings | Out-Null
52
-
53
- @ ($AuthColumnKeyName , $AppColumnKeyName , $LogColumnKeyName ) | % {
54
- New-SqlColumnEncryptionKey `
55
- - InputObject $smoDatabase `
56
- - ColumnMasterKey $MasterKeySQLName `
57
- - Name $_ | Out-Null
38
+ $Cert = (Get-ChildItem Cert:\CurrentUser\My | Where-Object subject -eq ' CN=Always Encrypted Sample Cert' ) | Select-Object Thumbprint - First 1
39
+ if ($Cert ) {
40
+ Write-Verbose " Certificate `" $ ( $MasterKeyDNSName ) `" Already exists"
41
+ }
42
+ 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 ) "
53
+ }
54
+
55
+ 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
59
+ }
60
+
61
+ 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
65
+ }
66
+
67
+ if ($smoDatabase.ColumnMasterKeys [' AlwaysEncryptedSampleCMK' ]) {
68
+ Write-Warning " Master Key Reference $ ( $MasterKeySQLName ) already exists in the database."
69
+ }
70
+ 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
77
+ }
78
+
79
+ $ExistingColumnKeys = $smoDatabase.ColumnEncryptionKeys
80
+ @ ($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
+ }
58
89
}
59
90
0 commit comments