简体   繁体   English

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

[英]Microsoft DacServices Powershell error on constructor when calling new-object

I have the following Powershell script I am trying to run:我正在尝试运行以下 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)

However, when it runs, I am getting the following error:但是,当它运行时,我收到以下错误:

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

I am trying to run this for an automated database deploy but cannot get past this weird error.我正在尝试为自动数据库部署运行它,但无法克服这个奇怪的错误。 I have already set my execution policy to remotesigned and updated my runtime version for Powershell to .NET 4.0.我已经将我的执行策略设置为 remotesigned 并将我的 Powershell 运行时版本更新为 .NET 4.0。 Can't figure out what else could be wrong.想不出还有什么问题。

Any help would be greatly appreciated!任何帮助将不胜感激!

The problem here is that the default authentication method is SQL Server authentication which expects a username and password.这里的问题是默认的身份验证方法是 SQL Server 身份验证,它需要用户名和密码。 You will need to either supply those parameters or explicitly specify that Windows authentication should be used.您将需要提供这些参数或明确指定应使用 Windows 身份验证。 You can do this by replacing your connection string argument with the following.您可以通过将连接字符串参数替换为以下内容来完成此操作。

"server=localhost;Integrated Security = True;"

Alternatively, you could use the following function to encapsulate this logic.或者,您可以使用以下函数来封装此逻辑。 Note that the default parameter set is 'WindowsAuthentication' which does not include the UserName or Password parameters.请注意,默认参数集是“WindowsAuthentication”,其中不包括 UserName 或 Password 参数。 If you supply either of these, Powershell will use the 'SqlServerAuthentication' parameter set and the $PSCmdlet.ParameterSetName variable will be set appropriately.如果您提供其中任何一个,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