-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathExport-SQLDacPAcs.ps1
More file actions
28 lines (22 loc) · 1 KB
/
Export-SQLDacPAcs.ps1
File metadata and controls
28 lines (22 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Export-SQLDacPacs{
param([string[]] $Instances = 'localhost',
[string] $outputdirectory=([Environment]::GetFolderPath("MyDocuments"))
)
#get the sqlpackage executable
$sqlpackage = (get-childitem C:\ -Recurse | Where-Object {$_.name -eq 'sqlpackage.exe'} |Sort-Object LastWriteTime | Select-Object -First 1).FullName
#declare a select query for databases
$dbsql = @"
SELECT name FROM sys.databases
where database_id >4 and state_desc = 'ONLINE'
"@
foreach($instance in $Instances){
$dbs = Invoke-Sqlcmd -ServerInstance $instance -Database tempdb -Query $dbsql
$datestring = (Get-Date -Format 'yyyyMMddHHmm')
$iname = $instance.Replace('\','_')
foreach($db in $dbs.name){
$outfile = Join-Path $outputdirectory -ChildPath "$iname-$db-$datestring.dacpac"
$cmd = "& '$sqlpackage' /action:Extract /targetfile:'$outfile' /SourceServerName:$instance /SourceDatabaseName:$db"
Invoke-Expression $cmd
}
}
}