[英]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部署失敗
2017-02-24T06:03:09.9785258Z ## [錯誤]異常使用“3”參數調用“Deploy”:“無法部署包”。
在D:\\ a \\ 1 \\ s ******************** \\ deploydatabase.ps1:104 char:13
+ $ dacService.Deploy($ dp,$ database,$ true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId:DacServicesException2017-02-24T06:03:10.0085256Z ## [錯誤]進程已完成,退出代碼為1,並且已將1個錯誤寫入錯誤流。
首先,您需要添加防火牆規則才能連接到Azure SQL Server。
碼:
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包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.