首页 > 解决方案 > Set-SqlColumnEncryption 抛出错误

问题描述

我正在使用 SSMS 中的加密列功能加密现有表中的几列。我选择生成一个 Powershell 脚本,而不是在向导中加密列,这样我就可以在稍后的时间点加密这些列。脚本如下:

# Generated by SQL Server Management Studio at 3:03 PM on 4/05/2018

Import-Module SqlServer
# Set up connection and database SMO objects

$sqlConnectionString = "Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Packet Size=4096;Application Name=`"Microsoft SQL Server Management Studio`""
$smoDatabase = Get-SqlDatabase -ConnectionString $sqlConnectionString

# If your encryption changes involve keys in Azure Key Vault, uncomment one of the lines below in order to authenticate:
#   * Prompt for a username and password:
#Add-SqlAzureAuthenticationContext -Interactive

#   * Enter a Client ID, Secret, and Tenant ID:
#Add-SqlAzureAuthenticationContext -ClientID '<Client ID>' -Secret '<Secret>' -Tenant '<Tenant ID>'

# Change encryption schema

$encryptionChanges = @()

# Add changes for table [dbo].[Voucher]
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.Voucher.Code -EncryptionType Randomized -EncryptionKey "cek"

Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $smoDatabase

Set-SqlColumnEncryption但是,当我运行脚本时,我从cmdlet中得到以下异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an
invocation. ---> System.TypeInitializationException: The type initializer for
'Microsoft.SqlServer.Management.AlwaysEncrypted.Management.AlwaysEncryptedManagement' threw an
exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json,
Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The
system cannot find the file specified.

我也更新了 Sqlserver 模块。当然,我不必手动将 Newtonsoft.Json.dll 文件放到 SqlServer 模块目录中。有任何想法吗?

标签: powershellalways-encrypted

解决方案


我遇到了完全相同的问题,我相信这是您特定的 OS 和 Sql Server 组合上的某种组件集成问题。

举个例子,当我试图在我的笔记本电脑上运行那个 powershell 脚本时,我收到了这个错误。我的笔记本电脑恰好使用 Windows 10 和 Sql Server Management Studio 17.2(最终生成 ps1)。此外,我的笔记本电脑确实在正确的目录中包含 Newtonsoft.Json.dll。

但是,我跳到了使用 Windows 2012 R2 和 SSMS 17.2 的服务器上,并且脚本可以正常工作!

最终,就好像在 Windows10/SSMS17.2 安装中缺少某种程序集绑定重定向,Windows2012R2/SSMS17.2 似乎可以正确解决。


推荐阅读