Skip to content

Instantly share code, notes, and snippets.

@ashtewari
Last active May 31, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashtewari/175a05c8d68bf3189c9bb6a460429bb3 to your computer and use it in GitHub Desktop.
Save ashtewari/175a05c8d68bf3189c9bb6a460429bb3 to your computer and use it in GitHub Desktop.
<#
Visual Studio 2015 build creates a dacpac file, but Azure Resource Manager templates can use only bacpac files.
This powershell script automates creation of bacpac and publishing to the blob storage.
After building the data project, open a command prompt on the folder containing the dacpac file.
Copy this script to the same folder.
You need :
1. Name of the localdb server where the dacpac is published ((localdb)\ProjectsV13)
2. Execute Get-AzurePublishSettingsFile and save the azure-account.publishsettings file in the same folder where dacpac file is located.
3. Storage Account Name
4. Storage Account Key.
To run this powershell script, open a command prompt and execute
powershell -noexit -File ".\create-bacpac.ps1"
#>
$ServerName="(localdb)\ProjectsV13"
$PubSettingsFile=".\azure_account.publishsettings"
$StorageAccountName="azure_storage_account_name"
$StorageAccountKey="azure_storage_account_key"
$DbName="MyDb"
$BacPacFileName="MyDb.bacpac"
# Publish DacPac to localdb
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\sqlpackage.exe" /Action:Publish /SourceFile:$DbName.dacpac /TargetDatabaseName:$DbName /TargetServerName:$ServerName
# Create BacPac from localdb
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\sqlpackage.exe" /a:Export /ssn:$ServerName /sdn:$DbName /tf:$BacPacFileName
# Copy BacPac to Azure Blob Storage
# 1. Execute Get-AzurePublishSettingsFile and save the publishsettings file.
# 2. Import publishsettings file
Import-AzurePublishSettingsFile $PubSettingsFile
# 3. Upload bacpac to blob storage
$context= New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Set-AzureStorageBlobContent -Context $context -Container packages -File $BacPacFileName -Blob $BacPacFileName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment