Skip to content

Instantly share code, notes, and snippets.

@crshnbrn66
Last active October 22, 2020 12:10
Show Gist options
  • Save crshnbrn66/eaa542053f180039ce25e58e92670998 to your computer and use it in GitHub Desktop.
Save crshnbrn66/eaa542053f180039ce25e58e92670998 to your computer and use it in GitHub Desktop.
param($dacpacPath = 'c:\dacpacPath', $dacpac = 'your.dacpac')
add-type -path 'C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\Microsoft.SqlServer.Dac.Extensions.dll'
cd $dacpacPath
$model =[Microsoft.SqlServer.Dac.Model.TSqlModel]::new(((get-item ".\$dacpac").fullname))
$queryScopes = [Microsoft.SqlServer.Dac.Model.DacQueryScopes]::All
$returnObjects = $model.GetObjects([Microsoft.SqlServer.Dac.Model.DacQueryScopes]::All)
$s = ''
foreach($r in $returnObjects)
{
if ($r.TryGetScript([ref]$s))
{
$objectTypeName = $r.ObjectType.Name;
$d="c:\temp\db\$objectTypeName"
if(!(test-path $d ))
{
new-item $d -ItemType Directory
}
$filename = "$d\$($r.Name.Parts).sql"
if (! (test-path $filename))
{
Try
{
new-item $filename -ItemType File -ErrorAction Stop -Force
}
Catch
{
$url = "$($r.Name.Parts)"
if ([system.uri]::IsWellFormedUriString($url, [system.urikind]::Absolute))
{
$u = ([uri]"$url").Segments[-1]
$filename = "$d\$u.sql"
new-item $filename -ItemType File -ErrorAction Stop -Force
}
}
}
$s | out-file $filename -Force
write-output $filename
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment