I'm not able to insert into an always encrypted table using PowerShell, the code I'm using is:
$serverName = "ServerName"
$databaseName = "SecureDB"
$connStr = "Server = " + $serverName + "; Database = " + $databaseName + ";
Integrated Security = True; Column Encryption Setting = Enabled"
$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]
$query = @"
DECLARE @Param1 VARCHAR(50) = 'Param1Value'
DECLARE @Param2 VARCHAR(500) = 'Param2Value'
DECLARE @Param3 VARCHAR(50) = 'Param3Value'
DECLARE @Param4 VARCHAR(100) = 'Param4Value'
Insert into [dbo].[SecureTable]
values (@Param1,@Param2,@Param3,@Param4)
GO
"@
Invoke-Sqlcmd -Query $query -ConnectionString $connStr
Note that only Param2 is encrypted and I'm using the same query on SSMS successfully.
Error message:
Invoke-Sqlcmd : Encryption scheme mismatch for columns/variables '@Param2'. The encryption scheme for the columns/variables is (encryption_type = 'PLAINTEXT') and the expression near line '6' expects it to be (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK1', column_encryption_key_database_name = 'SecureDB') (or weaker).