Hi,
I am using
1) System 1: Windows Server 2012 R2 and installed .Net Framework 4.6.1 on it, Powershell verison 5. Now I am trying to execute the powershell scripts from here, which connects to a
2) System 2: Windows 2012 Server R2 and having Server having SQL Server 2016 installed
3) My purpose is to encrypt the columns using powershell
4) I have connected to Powershell console using Administrator login in system 1 and database is successfully connected, since I debugged the code in powershell ISE and checked the same.
5) I have installed the Master Key certificate in System 1 and already created the "Master Key" metadata CMK1 and "Encryption Key" CEK1 in the database server successfully
6) I am getting exception as follows
==============================================================
Set-SqlColumnEncryption : The type initializer for 'Microsoft.SqlServer.Management.AlwaysEncrypted.Types.AlwaysEncryptedManager' threw an
exception.
At C:\DB\RMD_Customer_Encyption_DBA_Part.ps1:35 char:1
+ Set-SqlColumnEncryption -InputObject $database -ColumnEncryptionSetti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-SqlColumnEncryption], TypeInitializationException
+ FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.SqlServer.Management.PowerShell.AlwaysEncrypted.SetColumnEncryption
=====================================================================
Source code is
# Obtain the location of the column master key and the encrypted value of the column encryption key from your Security Administrator, via a CSV file on a share drive.$keyDataFile = "C:\db\keydata1.txt"
$keyData = Import-Csv $keyDataFile
# Import the SqlServer module
Import-Module "SqlServer"
# Connect to your database.
$serverName = "IN1"
$databaseName = "ABC"
$password = "Admin1234"
$connStr = "Data Source=IN1;Initial Catalog=ABC;User ID=sa;Password=Admin1234;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Packet Size=4096;Column Encryption Setting=Enabled"
#$connStr = "Server = " + $serverName + "; Database = " + $databaseName + "; Integrated Security = True"
$connection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection
$connection.ConnectionString = $connStr
$connection.Connect()
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($connection)
$database = $server.Databases[$databaseName]
# Encrypt the selected columns (or re-encrypt, if they are already encrypted using keys/encrypt types, different than the specified keys/types.
$ces = @()
$ces += New-SqlColumnEncryptionSettings -ColumnName "dbo.Customer.FirstName" -EncryptionType "Deterministic" -EncryptionKey "CEK1"
$ces += New-SqlColumnEncryptionSettings -ColumnName "dbo.Customer.LastName" -EncryptionType "Deterministic" -EncryptionKey "CEK1"
Set-SqlColumnEncryption -InputObject $database -ColumnEncryptionSettings $ces