Repository navigation
Expand file tree
/
Copy path23_Database_Diagrams.ps1
More file actions
191 lines (150 loc) · 4.62 KB
/
Copy path23_Database_Diagrams.ps1
File metadata and controls
191 lines (150 loc) · 4.62 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<#
.SYNOPSIS
Gets the Database Diagrams from the target server
.DESCRIPTION
Creates INSERT Statements into [database].[dbo].[sysdiagrams]
.EXAMPLE
23_Database_Diagrams.ps1 localhost
.Inputs
ServerName\instance, [SQLUser], [SQLPassword]
.Outputs
.NOTES
.LINK
https://github.com/gwalkey
#>
[CmdletBinding()]
Param(
[string]$SQLInstance="localhost",
[string]$myuser,
[string]$mypass
)
# Load Common Modules and .NET Assemblies
try
{
Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop
}
catch
{
Throw('SQLTranscriptase.psm1 not found')
}
LoadSQLSMO
# Init
Set-StrictMode -Version latest;
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
Write-Host -f Yellow -b Black "23 - Database Diagrams"
Write-Output("Server: [{0}]" -f $SQLInstance)
# Server connection check
$SQLCMD1 = "select serverproperty('productversion') as 'Version'"
try
{
if ($mypass.Length -ge 1 -and $myuser.Length -ge 1)
{
Write-Output "Testing SQL Auth"
$myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| select -ExpandProperty Version
$serverauth="sql"
}
else
{
Write-Output "Testing Windows Auth"
$myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | select -ExpandProperty Version
$serverauth = "win"
}
if($myver -ne $null)
{
Write-Output ("SQL Version: {0}" -f $myver)
}
}
catch
{
Write-Host -f red "$SQLInstance appears offline."
Set-Location $BaseFolder
exit
}
[int]$ver = GetSQLNumericalVersion $myver
# New UP SMO Object
if ($serverauth -eq "win")
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
}
else
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
$srv.ConnectionContext.LoginSecure=$false
$srv.ConnectionContext.set_Login($myuser)
$srv.ConnectionContext.set_Password($mypass)
}
# Create output folder
$output_path = "$BaseFolder\$SQLInstance\23 - Database Diagrams\"
if(!(test-path -path $output_path))
{
mkdir $output_path | Out-Null
}
# -----------------------
# iterate over each DB
# -----------------------
foreach($sqlDatabase in $srv.databases)
{
# Skip System Databases - unless you actually installed some USER Tables here- bad monkey
if ($sqlDatabase.Name -in 'Master','Model','MSDB','TempDB','SSISDB') {continue}
# Strip brackets from DBname
$db = $sqlDatabase
$fixedDBName = $db.name.replace('[','')
$fixedDBName = $fixedDBName.replace(']','')
$output_path = "$BaseFolder\$SQLInstance\23 - Database Diagrams\$fixedDBname"
# Get Diagrams
$sqlCMD2 =
"
USE [$fixedDBName];
if (select 1 from sys.tables where name = 'sysdiagrams')=1
begin
select [name], [principal_id], [version], [definition] from dbo.sysdiagrams
end
"
if ($serverauth -eq "win")
{
$sqlresults2 = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD2
}
else
{
$sqlresults2 = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD2 -User $myuser -Password $mypass
}
# None found? Skip
if (!$sqlresults2) {continue}
Write-Output ("Scripting out Database Diagrams for: {0}" -f $fixedDBName)
# One Output folder per DB
if(!(test-path -path $output_path))
{
mkdir $output_path | Out-Null
}
foreach ($diagram in $sqlresults2)
{
$DName = $diagram.name
$sqlCMD3 = "`
Use ["+$sqlDatabase.Name+"];"+
"
select
'insert into dbo.sysdiagrams ([name], [principal_id], [version], [definition]) values ('+
char(39)+[name]+ char(39)+', '+
convert(nvarchar,[principal_id])+', '+
convert(nvarchar,[Version])+', '+
'0x'+convert(varchar(max),[definition],2) +
')' as 'column1'
from dbo.sysdiagrams
where [name] = '$DName'
"
# Dump Diagrams
if ($serverauth -eq "win")
{
$sqlresults3 = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD3
}
else
{
$sqlresults3 = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $sqlCMD3 -User $myuser -Password $mypass
}
# Write Out
$myoutputfile = $output_path+"\"+$DName+".sql"
$sqlresults3.column1 | out-file -FilePath $myoutputfile -append -encoding ascii -width 10000000
}
}
# Return To Base
set-location $BaseFolder