繁体   English   中英

调用新对象时构造函数上的 Microsoft DacServices Powershell 错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM