-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport.ps1
More file actions
52 lines (47 loc) · 1.45 KB
/
Export.ps1
File metadata and controls
52 lines (47 loc) · 1.45 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Connectivity details User DB and master DB
$server= "localhost"
$user="sa"
$password="********"
$Folder="C:\SqlPackage\Files\"
#Function to connect to the database
Function GiveMeConnectionSource{
for ($i=0; $i -lt 10; $i++)
{
try
{
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SQLConnection.ConnectionString = "Server="+$server+";Database=master"+";User ID="+$user+";Password="+$password+";Connection Timeout=60"
$SQLConnection.Open()
break;
}
catch
{
Start-Sleep -s 5
}
}
Write-output $SQLConnection
}
try
{
Clear-Host
$SQLConnectionSource = GiveMeConnectionSource
$SQLCommandExiste = New-Object System.Data.SqlClient.SqlCommand("Select Name from sys.databases Where database_id>=5 and name <>'distribution' Order by Name", $SQLConnectionSource)
$Reader = $SQLCommandExiste.ExecuteReader();
while($Reader.Read())
{
$ComandoOut="c:\SqlPackage\Script\Export.bat " + $server + " " + $Reader.GetSqlString(0).ToString() + " " + $user + " " + $password + " " + $Folder+$Reader.GetSqlString(0).ToString() + ".bacpac"
Write-Host $ComandoOut
Invoke-Expression -Command $ComandoOut
}
$Reader.Close();
$SQLConnectionSource.Close()
}
catch
{
Write-Host -ForegroundColor DarkYellow "Error:"
Write-Host -ForegroundColor Magenta $Error[0].Exception
}
finally
{
Write-Host -ForegroundColor Cyan "Process finished"
}