通过 power shell 脚本将 Dacpac 包部署到 Azure SQL 服务器
Deploy Dacpac packages via power shell script to Azure SQL Server
我正在尝试使用 PowerShell 脚本在单个构建过程中部署多个 dacpac。
param(
[string]$publish_profile,
[string]$path_to_snapshots,
[string]$password
)
#Load Microsoft.SqlServer.Dac assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Dac")
#Load Dac profile
$dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($publish_profile)
$dacService = new-object Microsoft.SqlServer.Dac.DacServices($dacProfile.TargetConnectionString)
$files = Get-ChildItem "$path_to_snapshots\*.dacpac"
foreach ($file in $files)
{
$fileName = $file.Name
Try
{
$dp = [Microsoft.SqlServer.Dac.DacPackage]::Load($file.FullName)
$dacService.Deploy($dp, $database, $true)
}
}
Catch
{
Write-Host "$fileName deployment has been failed" -foregroundcolor "red"
throw $_.Exception;
Break
}
}
在我的本地环境中一切正常,但在 Visual Studio 团队服务的构建过程中我收到错误:
2017-02-24T06:03:09.7955300Z *********.dacpac deployment has been failed
2017-02-24T06:03:09.9785258Z ##[error]Exception calling "Deploy" with "3" argument(s): "Could not deploy package."
At D:\a\s********************\deploydatabase.ps1:104 char:13
+ $dacService.Deploy($dp, $database, $true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DacServicesException
2017-02-24T06:03:10.0085256Z ##[error]Process completed with exit code 1 and had 1 error(s) written to the error stream.
首先,您需要添加防火墙规则才能连接到 Azure SQL 服务器。
- 编辑您的构建定义
- Select 选项选项卡并选中允许脚本访问 OAuth 令牌
- 添加 Azure PowerShell 步骤(参数:-RestAddress https://[account].vsdtl.visualstudio.com/DefaultCollection/_apis/vslabs/ipaddress -Token $(System.AccessToken) -RG [资源组] -Server [服务器名称] -ruleName $(Build.BuildNumber )
代码:
param (
[string]$RestAddress,
[string]$Token,
[string]$RG,
[string]$Server
)
$basicAuth = ("{0}:{1}" -f 'test',$Token)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}" -f $basicAuth)}
$result = Invoke-RestMethod -Uri $RestAddress -headers $headers -Method Get
Write-Host $result.value
New-AzureRmSqlServerFirewallRule -ResourceGroupName $RG -ServerName $Server -FirewallRuleName "UnitTestRule" -StartIpAddress "$($result.value)" -EndIpAddress "$($result.value)"
其次,我推荐你使用这个包中的程序集:
Microsoft.SqlServer.Dac.
第三,要获取详细错误,您可以改用此代码:
Catch
{
Write-Host "$fileName deployment has been failed" -foregroundcolor "red"
$Error | format-list -force
Write-Host $Error[0].Exception.ParentContainsErrorRecordException;
Break
}
另一方面,我建议您可以通过 SqlPackage.exe 部署 SQL 包。
我正在尝试使用 PowerShell 脚本在单个构建过程中部署多个 dacpac。
param(
[string]$publish_profile,
[string]$path_to_snapshots,
[string]$password
)
#Load Microsoft.SqlServer.Dac assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Dac")
#Load Dac profile
$dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($publish_profile)
$dacService = new-object Microsoft.SqlServer.Dac.DacServices($dacProfile.TargetConnectionString)
$files = Get-ChildItem "$path_to_snapshots\*.dacpac"
foreach ($file in $files)
{
$fileName = $file.Name
Try
{
$dp = [Microsoft.SqlServer.Dac.DacPackage]::Load($file.FullName)
$dacService.Deploy($dp, $database, $true)
}
}
Catch
{
Write-Host "$fileName deployment has been failed" -foregroundcolor "red"
throw $_.Exception;
Break
}
}
在我的本地环境中一切正常,但在 Visual Studio 团队服务的构建过程中我收到错误:
2017-02-24T06:03:09.7955300Z *********.dacpac deployment has been failed
2017-02-24T06:03:09.9785258Z ##[error]Exception calling "Deploy" with "3" argument(s): "Could not deploy package."
At D:\a\s********************\deploydatabase.ps1:104 char:13
+ $dacService.Deploy($dp, $database, $true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DacServicesException2017-02-24T06:03:10.0085256Z ##[error]Process completed with exit code 1 and had 1 error(s) written to the error stream.
首先,您需要添加防火墙规则才能连接到 Azure SQL 服务器。
- 编辑您的构建定义
- Select 选项选项卡并选中允许脚本访问 OAuth 令牌
- 添加 Azure PowerShell 步骤(参数:-RestAddress https://[account].vsdtl.visualstudio.com/DefaultCollection/_apis/vslabs/ipaddress -Token $(System.AccessToken) -RG [资源组] -Server [服务器名称] -ruleName $(Build.BuildNumber )
代码:
param (
[string]$RestAddress,
[string]$Token,
[string]$RG,
[string]$Server
)
$basicAuth = ("{0}:{1}" -f 'test',$Token)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}" -f $basicAuth)}
$result = Invoke-RestMethod -Uri $RestAddress -headers $headers -Method Get
Write-Host $result.value
New-AzureRmSqlServerFirewallRule -ResourceGroupName $RG -ServerName $Server -FirewallRuleName "UnitTestRule" -StartIpAddress "$($result.value)" -EndIpAddress "$($result.value)"
其次,我推荐你使用这个包中的程序集: Microsoft.SqlServer.Dac.
第三,要获取详细错误,您可以改用此代码:
Catch
{
Write-Host "$fileName deployment has been failed" -foregroundcolor "red"
$Error | format-list -force
Write-Host $Error[0].Exception.ParentContainsErrorRecordException;
Break
}
另一方面,我建议您可以通过 SqlPackage.exe 部署 SQL 包。