Skip to content

Commit

Permalink
updated docs, cleaned up code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Dec 14, 2016
1 parent cb715ad commit 8f97728
Showing 1 changed file with 127 additions and 129 deletions.
256 changes: 127 additions & 129 deletions functions/Export-DbaAvailabiltyGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
{
<#
.SYNOPSIS
Export SQL Server Availability Groups to a T-SQL file.
Exports SQL Server Availability Groups to a T-SQL file.
.DESCRIPTION
Export SQL Server Availability Groups creation scripts to a T-SQL file. This is a function that is not available in SSMS.
THIS CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
Exports SQL Server Availability Groups creation scripts to a T-SQL file. This is a function that is not available in SSMS.
.PARAMETER SqlServer
The SQL Server instance name. SQL Server 2012 and above supported.
Expand Down Expand Up @@ -37,151 +35,151 @@ Confirms each step/line of output
.NOTES
Author: Chris Sommer (@cjsommer), cjsommmer.com
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
dbatools PowerShell module (https://dbatools.io)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
.LINK
https://dbatools.io/Export-DbaAvailabilityGroup
.EXAMPLE
Export-DbaAvailabilityGroup -SqlServer sql2012 -FilePath 'C:\temp\availability_group_exports'
Exports all Availability Groups from SQL server "sql2012". Output scripts are witten to the C:\temp\availability_group_exports directory.
Export-DbaAvailabilityGroup -SqlServer sql2012
Exports all Availability Groups from SQL server "sql2012". Output scripts are written to the Documents\SqlAgExports directory by default.
.EXAMPLE
Export-DbaAvailabilityGroup -SqlServer sql2012 -FilePath 'C:\temp\availability_group_exports' -AvailabilityGroups AG1,AG2
Export-DbaAvailabilityGroup -SqlServer sql2012 -FilePath C:\temp\availability_group_exports
Exports Availability Groups AG1 and AG2 from SQL server "sql2012". Output scripts are witten to the C:\temp\availability_group_exports directory.
Exports all Availability Groups from SQL server "sql2012". Output scripts are written to the C:\temp\availability_group_exports directory.
.EXAMPLE
Export-DbaAvailabilityGroup -SqlServer sql2014 -FilePath 'C:\temp\availability_group_exports' -NoClobber
Export-DbaAvailabilityGroup -SqlServer sql2012 -FilePath 'C:\dir with spaces\availability_group_exports' -AvailabilityGroups AG1,AG2
Exports all Availability Groups from SQL server "sql2014". Output scripts are witten to the C:\temp\availability_group_exports directory. If the export file already exists it will not be overwritten.
Exports Availability Groups AG1 and AG2 from SQL server "sql2012". Output scripts are written to the C:\dir with spaces\availability_group_exports directory.
.LINK
https://dbatools.io/Export-DbaAvailabilityGroup
.EXAMPLE
Export-DbaAvailabilityGroup -SqlServer sql2014 -FilePath C:\temp\availability_group_exports -NoClobber
Exports all Availability Groups from SQL server "sql2014". Output scripts are written to the C:\temp\availability_group_exports directory. If the export file already exists it will not be overwritten.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
[CmdletBinding(SupportsShouldProcess = $true)]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlInstance")]
[object[]]$SqlServer,

[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlInstance")]
[object[]]$SqlServer,
[System.Management.Automation.PSCredential]$SqlCredential,

[Alias("OutputLocation", "Path")]
[string]$FilePath,

[switch]$NoClobber
[string]$FilePath = "$([Environment]::GetFolderPath("MyDocuments"))\SqlAgExport",
[switch]$NoClobber
)

DynamicParam { if ($SqlServer) { return Get-ParamSqlAvailabilityGroups -SqlServer $SqlServer -SqlCredential $SqlCredential } }

BEGIN
{
Write-Output "Beginning Export-DbaAvailabilityGroup on '$SqlServer'"
$AvailabilityGroups = $PSBoundParameters.AvailabilityGroups

Write-Verbose "Connecting to SqlServer '$SqlServer'"

try {
$server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
} catch {
if ($server.count -eq 1) {
throw $_
} else {
Write-Warning "Can't connect to $SqlServer. Moving on."
Continue
}
}
}

PROCESS
{
# Get all of the Availability Groups and filter if required
$AllAGs = $server.AvailabilityGroups

if ($AvailabilityGroups) {
Write-Verbose 'Filtering AvailabilityGroups'
$AllAGs = $AllAGs | Where-Object {$_.name -in $AvailabilityGroups}
}

if ($AllAGs.count -gt 0) {

# Set and create the OutputLocation if it doesn't exist
$SQLINST = $SQLServer.Replace('\','$')
$OutputLocation = "${FilePath}\${SQLINST}"

if (!(Test-Path $OutputLocation -PathType Container)) {
New-Item -Path $OutputLocation -ItemType Directory -Force | Out-Null
}

# Script each Availability Group
foreach ($ag in $AllAGs) {
$AGName = $ag.Name

# Set the outfile name
if ($AppendDateToOutputFilename.IsPresent) {
$Dttm = (Get-Date -Format 'yyyyMMdd_hhmm')
$OutFile = "${OutputLocation}\${AGname}_${Dttm}.sql"
} else {
$OutFile = "${OutputLocation}\${AGname}.sql"
}

# Check NoClobber and script out the AG
if ($NoClobber.IsPresent -and (Test-Path -Path $OutFile -PathType Leaf)) {
Write-Warning "OutputFile '$OutFile' already exists. Skipping due to -NoClobber parameter"
} else {
Write-output "Scripting Availability Group [$AGName] on [$SQLServer] to '$OutFile'"

# Create comment block header for AG script
"/*" | Out-File -FilePath $OutFile -Encoding ASCII -Force
" * Created by dbatools 'Export-DbaAvailabilityGroup' cmdlet on '$(Get-Date)'" | Out-File -FilePath $OutFile -Encoding ASCII -Append
" * See https://dbatools.io/Export-DbaAvailabilityGroup for more help" | Out-File -FilePath $OutFile -Encoding ASCII -Append

# Output AG and listener names
" *" | Out-File -FilePath $OutFile -Encoding ASCII -Append
" * Availability Group Name: $($ag.name)" | Out-File -FilePath $OutFile -Encoding ASCII -Append
$ag.AvailabilityGroupListeners | % {" * Listener Name: $($_.name)"} | Out-File -FilePath $OutFile -Encoding ASCII -Append

# Output all replicas
" *" | Out-File -FilePath $OutFile -Encoding ASCII -Append
$ag.AvailabilityReplicas | % {" * Replica: $($_.name)"} | Out-File -FilePath $OutFile -Encoding ASCII -Append

# Output all databases
" *" | Out-File -FilePath $OutFile -Encoding ASCII -Append
$ag.AvailabilityDatabases | % {" * Database: $($_.name)"} | Out-File -FilePath $OutFile -Encoding ASCII -Append

# $ag | Select-Object -Property * | Out-File -FilePath $OutFile -Encoding ASCII -Append

"*/" | Out-File -FilePath $OutFile -Encoding ASCII -Append

# Script the AG
$ag.Script() | Out-File -FilePath $OutFile -Encoding ASCII -Append
}
}
} else {
Write-Output "No Availability Groups detected on '$SqlServer'"
}
}

END
{
BEGIN
{
Write-Output "Beginning Export-DbaAvailabilityGroup on $SqlServer"
$AvailabilityGroups = $PSBoundParameters.AvailabilityGroups

Write-Verbose "Connecting to SqlServer $SqlServer"

try
{
$server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
}
catch
{
Write-Warning "Can't connect to $SqlServer. Moving on."
Continue
}
}

PROCESS
{
# Get all of the Availability Groups and filter if required
$allags = $server.AvailabilityGroups

if ($AvailabilityGroups)
{
Write-Verbose 'Filtering AvailabilityGroups'
$allags = $allags | Where-Object { $_.name -in $AvailabilityGroups }
}

if ($allags.count -gt 0)
{

# Set and create the OutputLocation if it doesn't exist
$sqlinst = $SQLServer.Replace('\', '$')
$OutputLocation = "$FilePath\$sqlinst"

if (!(Test-Path $OutputLocation -PathType Container))
{
New-Item -Path $OutputLocation -ItemType Directory -Force | Out-Null
}

# Script each Availability Group
foreach ($ag in $allags)
{
$agname = $ag.Name

# Set the outfile name
if ($AppendDateToOutputFilename.IsPresent)
{
$formatteddate = (Get-Date -Format 'yyyyMMdd_hhmm')
$outfile = "$OutputLocation\${AGname}_${formatteddate}.sql"
}
else
{
$outfile = "$OutputLocation\$agname.sql"
}

# Check NoClobber and script out the AG
if ($NoClobber.IsPresent -and (Test-Path -Path $outfile -PathType Leaf))
{
Write-Warning "OutputFile $outfile already exists. Skipping due to -NoClobber parameter"
}
else
{
Write-output "Scripting Availability Group [$agname] on $SQLServer to $outfile"

# Create comment block header for AG script
"/*" | Out-File -FilePath $outfile -Encoding ASCII -Force
" * Created by dbatools 'Export-DbaAvailabilityGroup' cmdlet on '$(Get-Date)'" | Out-File -FilePath $outfile -Encoding ASCII -Append
" * See https://dbatools.io/Export-DbaAvailabilityGroup for more help" | Out-File -FilePath $outfile -Encoding ASCII -Append

# Output AG and listener names
" *" | Out-File -FilePath $outfile -Encoding ASCII -Append
" * Availability Group Name: $($ag.name)" | Out-File -FilePath $outfile -Encoding ASCII -Append
$ag.AvailabilityGroupListeners | ForEach-Object { " * Listener Name: $($_.name)" } | Out-File -FilePath $outfile -Encoding ASCII -Append

# Output all replicas
" *" | Out-File -FilePath $outfile -Encoding ASCII -Append
$ag.AvailabilityReplicas | ForEach-Object { " * Replica: $($_.name)" } | Out-File -FilePath $outfile -Encoding ASCII -Append

# Output all databases
" *" | Out-File -FilePath $outfile -Encoding ASCII -Append
$ag.AvailabilityDatabases | ForEach-Object { " * Database: $($_.name)" } | Out-File -FilePath $outfile -Encoding ASCII -Append

# $ag | Select-Object -Property * | Out-File -FilePath $outfile -Encoding ASCII -Append

"*/" | Out-File -FilePath $outfile -Encoding ASCII -Append

# Script the AG
$ag.Script() | Out-File -FilePath $outfile -Encoding ASCII -Append
}
}
}
else
{
Write-Output "No Availability Groups detected on $SqlServer"
}
}

END
{
$server.ConnectionContext.Disconnect()
If ($Pscmdlet.ShouldProcess("console", "Showing finished message")) { Write-Output "Completed Export-DbaAvailabilityGroup on '$SqlServer'" }
}
If ($Pscmdlet.ShouldProcess("console", "Showing finished message")) { Write-Output "Completed Export-DbaAvailabilityGroup on $SqlServer" }
}
}

0 comments on commit 8f97728

Please sign in to comment.