forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport-DbaLinkedServer.ps1
170 lines (137 loc) · 6.8 KB
/
Export-DbaLinkedServer.ps1
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
function Export-DbaLinkedServer {
<#
.SYNOPSIS
Exports linked servers INCLUDING PASSWORDS, unless specified otherwise, to sql file.
.DESCRIPTION
Exports linked servers INCLUDING PASSWORDS, unless specified otherwise, to sql file.
Requires remote Windows access if exporting the password.
.PARAMETER SqlInstance
Source SQL Server. You must have sysadmin access and server version must be SQL Server version 2005 or higher.
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
.PARAMETER Credential
Login to the target OS using alternative linked servers. Accepts credential objects (Get-Credential)
.PARAMETER Path
Specifies the directory where the file or files will be exported.
.PARAMETER FilePath
Specifies the full file path of the output file.
.PARAMETER LinkedServer
The linked server(s) to export. If unspecified, all linked servers will be processed.
.PARAMETER InputObject
Allow credentials to be piped in from Get-DbaLinkedServer
.PARAMETER ExcludePassword
Exports the linked server without any sensitive information.
.PARAMETER Append
Append to Path
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: LinkedServer
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Export-DbaLinkedServer
.EXAMPLE
PS C:\> Export-DbaLinkedServer -SqlInstance sql2017 -Path C:\temp\ls.sql
Exports the linked servers, including passwords, from sql2017 to the file C:\temp\ls.sql
.EXAMPLE
PS C:\> Export-DbaLinkedServer -SqlInstance sql2017 -Path C:\temp\ls.sql -ExcludePassword
Exports the linked servers, without passwords, from sql2017 to the file C:\temp\ls.sql
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[string[]]$LinkedServer,
[PSCredential]$SqlCredential,
[PSCredential]$Credential,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[Alias("OutFile", "FileName")]
[string]$FilePath,
[switch]$ExcludePassword,
[switch]$Append,
[Microsoft.SqlServer.Management.Smo.LinkedServer[]]$InputObject,
[switch]$EnableException
)
begin {
$null = Test-ExportDirectory -Path $Path
}
process {
if (Test-FunctionInterrupt) { return }
if ($IsLinux -or $IsMacOS) {
Stop-Function -Message "This command is not supported on Linux or macOS"
return
}
foreach ($instance in $SqlInstance) {
try {
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
$InputObject += $server.LinkedServers
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
if ($LinkedServer) {
$InputObject = $InputObject | Where-Object Name -in $LinkedServer
}
if (-not $InputObject) {
Write-Message -Level Verbose -Message "Nothing to export"
continue
}
if (!(Test-SqlSa -SqlInstance $instance -SqlCredential $SqlCredential)) {
Stop-Function -Message "Not a sysadmin on $instance. Quitting." -Target $instance -Continue
}
Write-Message -Level Verbose -Message "Getting FullComputerName name for $instance."
$fullComputerName = Resolve-DbaComputerName -ComputerName $instance -Credential $Credential
Write-Message -Level Verbose -Message "Checking if Remote Registry is enabled on $instance."
try {
Invoke-Command2 -Raw -Credential $Credential -ComputerName $fullComputerName -ScriptBlock { Get-ItemProperty -Path "HKLM:\SOFTWARE\" } -ErrorAction Stop
} catch {
Stop-Function -Message "Can't connect to registry on $instance." -Target $fullComputerName -ErrorRecord $_
return
}
$FilePath = Get-ExportFilePath -Path $PSBoundParameters.Path -FilePath $PSBoundParameters.FilePath -Type sql -ServerName $instance
$sql = @()
if ($ExcludePassword) {
$sql += $InputObject.Script()
} else {
try {
$decrypted = Get-DecryptedObject -SqlInstance $server -Type LinkedServer
} catch {
Stop-Function -Continue -Message "Failure" -ErrorRecord $_
}
foreach ($ls in $InputObject) {
$currentls = $decrypted | Where-Object Name -eq $ls.Name
if ($currentls.Password) {
$tempsql = $ls.Script()
foreach ($map in $currentls) {
if ($map.Identity -isnot [dbnull]) {
$rmtuser = $map.Identity.Replace("'", "''")
$password = $map.Password.Replace("'", "''")
}
$tempsql = $tempsql.Replace(' /* For security reasons the linked server remote logins password is changed with ######## */', '')
$tempsql = $tempsql.Replace("rmtuser=N'$rmtuser',@rmtpassword='########'", "rmtuser=N'$rmtuser',@rmtpassword='$password'")
}
$sql += $tempsql
} else {
$sql += $ls.Script()
}
}
}
try {
if ($Append) {
Add-Content -Path $FilePath -Value $sql
} else {
Set-Content -Path $FilePath -Value $sql
}
Get-ChildItem -Path $FilePath
} catch {
Stop-Function -Message "Can't write to $FilePath" -ErrorRecord $_ -Continue
}
}
}
}