简体   繁体   中英

Microsoft DacServices Powershell error on constructor when calling new-object

I have the following Powershell script I am trying to run:

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. 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. You will need to either supply those parameters or explicitly specify that Windows authentication should be used. 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. If you supply either of these, Powershell will use the 'SqlServerAuthentication' parameter set and the $PSCmdlet.ParameterSetName variable will be set appropriately.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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