[英]Microsoft DacServices Powershell error on constructor when calling new-object
我正在嘗試運行以下 Powershell 腳本:
add-type -path "C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\Microsoft.SqlServer.Dac.dll";
$d = new-object Microsoft.SqlServer.Dac.DacServices "server=localhost"
# Load dacpac from file & deploy to database named pubsnew
$dp = [microsoft.sqlserver.dac.dacpackage]::load("c:\deploy\MyDbDacPac.dacpac")
$d.deploy($dp, "MyDb", $true)
但是,當它運行時,我收到以下錯誤:
New-Object : Exception calling ".ctor" with "1" argument(s): "The type initializer for 'Microsoft.SqlServer.Dac.DacServices' threw an exception."
At C:\Scripts\DeployDacPac.ps1:3 char:16
+ $d = new-object <<<< Microsoft.SqlServer.Dac.DacServices "server=localhost"
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : Cons tructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
我正在嘗試為自動數據庫部署運行它,但無法克服這個奇怪的錯誤。 我已經將我的執行策略設置為 remotesigned 並將我的 Powershell 運行時版本更新為 .NET 4.0。 想不出還有什么問題。
任何幫助將不勝感激!
這里的問題是默認的身份驗證方法是 SQL Server 身份驗證,它需要用戶名和密碼。 您將需要提供這些參數或明確指定應使用 Windows 身份驗證。 您可以通過將連接字符串參數替換為以下內容來完成此操作。
"server=localhost;Integrated Security = True;"
或者,您可以使用以下函數來封裝此邏輯。 請注意,默認參數集是“WindowsAuthentication”,其中不包括 UserName 或 Password 參數。 如果您提供其中任何一個,Powershell 將使用 'SqlServerAuthentication' 參數集,並且 $PSCmdlet.ParameterSetName 變量將被適當設置。
function Get-DacServices()
{
[CmdletBinding(DefaultParameterSetName="WindowsAuthentication")]
Param(
[string]$ServerName = 'localhost',
[Parameter(ParameterSetName='SqlServerAuthentication')]
[string]$UserName,
[Parameter(ParameterSetName='SqlServerAuthentication')]
[string]$Password
)
$connectionString = "server=$serverName;";
if($PSCmdlet.ParameterSetName -eq 'SqlServerAuthentication')
{
$connectionString += "User ID=$databaseUsername;Password=$databasePassword;";
}
else
{
$connectionString += "Integrated Security = True;";
}
$result = new-object Microsoft.SqlServer.Dac.DacServices $connectionString;
return $result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.