forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport-DbaRegServer.ps1
180 lines (144 loc) · 8.49 KB
/
Export-DbaRegServer.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
171
172
173
174
175
176
177
178
179
180
function Export-DbaRegServer {
<#
.SYNOPSIS
Exports registered servers and registered server groups to file
.DESCRIPTION
Exports registered servers and registered server groups to file
.PARAMETER SqlInstance
The target SQL Server instance or instances.
.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 CredentialPersistenceType
Used to specify how the login and passwords are persisted. Valid values include None, PersistLoginName and PersistLoginNameAndPassword.
.PARAMETER Path
Specifies the directory where the file or files will be exported.
.PARAMETER FilePath
Specifies the full file path of the output file. The file must end with .xml or .regsrvr
.PARAMETER InputObject
Enables piping from Get-DbaRegServer, Get-DbaRegServerGroup, CSVs and other objects.
If importing from CSV or other object, a column named ServerName is required. Optional columns include Name, Description and Group.
.PARAMETER Group
Specifies one or more groups to include.
.PARAMETER ExcludeGroup
Specifies one or more groups to exclude.
.PARAMETER Overwrite
Specifies to overwrite the output file (FilePath) if it already exists.
.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: RegisteredServer, CMS
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-DbaRegServer
.EXAMPLE
PS C:\> Export-DbaRegServer -SqlInstance sql2008
Exports all Registered Server and Registered Server Groups on sql2008 to an automatically generated file name in the current directory
.EXAMPLE
PS C:\> Get-DbaRegServer -SqlInstance sql2008, sql2012 | Export-DbaRegServer
Exports all registered servers on sql2008 and sql2012. Warning - each one will have its own individual file. Consider piping groups.
.EXAMPLE
PS C:\> Get-DbaRegServerGroup -SqlInstance sql2008, sql2012 | Export-DbaRegServer
Exports all registered servers on sql2008 and sql2012, organized by group.
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "", Justification = "For Parameter CredentialPersistenceType")]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[parameter(ValueFromPipeline)]
[object[]]$InputObject,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[Alias("OutFile", "FileName")]
[System.IO.FileInfo]$FilePath,
[ValidateSet("None", "PersistLoginName", "PersistLoginNameAndPassword")]
[string]$CredentialPersistenceType = "None",
[object[]]$Group,
[object[]]$ExcludeGroup,
[switch]$Overwrite,
[switch]$EnableException
)
begin {
$null = Test-ExportDirectory -Path $Path
$timeNow = (Get-Date -UFormat "%m%d%Y%H%M%S")
# ValidateScript in the above param block relies on the order of the params specified by the user,
# so the creation of the file path and $Overwrite are evaluated here
if ($PSBoundParameters.ContainsKey("FilePath")) {
if ($FilePath.FullName -notmatch "\.xml$|\.regsrvr$") {
Stop-Function -Message "The FilePath specified must end with either .xml or .regsrvr"
return
}
if (-not (Test-Path $FilePath) ) {
New-Item -Path $FilePath.DirectoryName -ItemType "directory" -Force | Out-Null # make sure the parent dir exists
} elseif (-not $Overwrite.IsPresent) {
Stop-Function -Message "Use the -Overwrite parameter if the file $FilePath should be overwritten."
return
}
}
}
process {
if (Test-FunctionInterrupt) { return }
foreach ($instance in $SqlInstance) {
if ($PSBoundParameters.ContainsKey("Group")) {
if ($PSBoundParameters.ContainsKey("ExcludeGroup")) {
$InputObject += Get-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Group $Group -ExcludeGroup $ExcludeGroup
} else {
$InputObject += Get-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Group $Group
}
} elseif ($PSBoundParameters.ContainsKey("ExcludeGroup")) {
$InputObject += Get-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -ExcludeGroup $ExcludeGroup
} else {
$InputObject += Get-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Id 1 # legacy behavior to return -Id 1 which means return everything
}
}
foreach ($object in $InputObject) {
try {
if ($object -is [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore]) {
if ($PSBoundParameters.ContainsKey("Group")) {
if ($PSBoundParameters.ContainsKey("ExcludeGroup")) {
$object = Get-DbaRegServerGroup -SqlInstance $object.ParentServer -Group $Group -ExcludeGroup $ExcludeGroup
} else {
$object = Get-DbaRegServerGroup -SqlInstance $object.ParentServer -Group $Group
}
} elseif ($PSBoundParameters.ContainsKey("ExcludeGroup")) {
$InputObject += Get-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -ExcludeGroup $ExcludeGroup
} else {
$object = Get-DbaRegServerGroup -SqlInstance $object.ParentServer -Id 1 # legacy behavior to return -Id 1 which means return everything
}
}
if (($object -is [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer]) -or ($object -is [Microsoft.SqlServer.Management.RegisteredServers.ServerGroup])) {
$regname = $object.Name.Replace('\', '$')
$OutputFilePath = $null
if (-not $PSBoundParameters.ContainsKey("FilePath")) {
$ExportFileName = $null
$serverName = $object.SqlInstance.Replace('\', '$');
if ($object -is [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer]) {
$ExportFileName = "$serverName-regserver-$regname-$timeNow.xml"
} elseif ($object -is [Microsoft.SqlServer.Management.RegisteredServers.ServerGroup]) {
$ExportFileName = "$serverName-reggroup-$regname-$timeNow.xml"
}
$OutputFilePath = Join-DbaPath -Path $Path -Child $ExportFileName
} elseif ($InputObject.length -gt 1) {
# more than one group was passed in, so we need to add the group name to the FilePath because there will be multiple files generated.
$extension = [IO.Path]::GetExtension($FilePath.FullName)
$OutputFilePath = $FilePath.FullName.Replace($extension, "-" + $regname + $extension)
} else {
$OutputFilePath = $FilePath.FullName
}
$object.Export($OutputFilePath, $CredentialPersistenceType)
Get-ChildItem $OutputFilePath -ErrorAction Stop
} else {
Stop-Function -Message "InputObject is not a registered server or server group" -Continue
}
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_
}
}
}
}