Skip to content

Commit 681cf61

Browse files
Added
1 parent 5f782ba commit 681cf61

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

Backup-OLADatabase.ps1

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<#
2+
.Synopsis
3+
To perform an ad-hoc backup using OLA Hallengrens solution
4+
.DESCRIPTION
5+
This function will perform an ad-hoc backup using OLA Hallengrens solution - It doesnot expose all of the
6+
capabilities but is useful for quick backups when needed
7+
Note the Database paramter is dynamically filled once you type it
8+
.EXAMPLE
9+
Backup-OlaDatabase -instance SERVERNAME -Type LOG -Share \\backupshare -Database Database
10+
11+
This will perform a LOG backup of the Database database on the SERVERNAME instance to the \\backupshare Share.
12+
Note the Database paramter is dynamically filled once you type it
13+
.EXAMPLE
14+
Backup-OlaDatabase -instance SERVERNAME -Type FULL -Share X:\backups -Database Database
15+
16+
This will perform a FULL backup of the Database database on the SERVERNAME instance to the X:\backups Share.
17+
Note the Database paramter is dynamically filled once you type it
18+
.EXAMPLE
19+
Backup-OlaDatabase -instance SERVERNAME -Type DIFF -Share \\TheShareForBackups -Database Database -OutputResults
20+
21+
This will perform a DIFF backup of the Database database on the SERVERNAME instance to the \\TheShareForBackups
22+
Share and output the message to the screen.
23+
Note the Database paramter is dynamically filled once you type it
24+
.NOTES
25+
26+
IF YOU HAVE THE STORED PROCEDURE IN A DIFFERENT DATABASE - You will need to alter line 130
27+
28+
AUTHOR - Rob Sewell https://sqldbawithabeard.com
29+
DATE - 27/10/2016
30+
#>
31+
function Backup-OlaDatabase
32+
{
33+
[CmdletBinding( SupportsShouldProcess=$true,
34+
PositionalBinding=$false,
35+
ConfirmImpact='Medium')]
36+
[Alias()]
37+
Param
38+
(
39+
# The Server/instance
40+
[Parameter(Mandatory=$true,HelpMessage='This is the Instance that the database is on',
41+
ValueFromPipeline=$true,
42+
ValueFromPipelineByPropertyName=$true,
43+
ValueFromRemainingArguments=$false,
44+
Position=0)]
45+
[ValidateNotNull()]
46+
[ValidateNotNullOrEmpty()]
47+
[string]$Instance,
48+
# The Server/instance
49+
[Parameter(Mandatory=$true,HelpMessage='This is the type of backup',
50+
ValueFromPipeline=$true,
51+
ValueFromPipelineByPropertyName=$true,
52+
ValueFromRemainingArguments=$false,
53+
Position=2)]
54+
[ValidateNotNull()]
55+
[ValidateNotNullOrEmpty()]
56+
[ValidateSet('FULL', 'DIFF', 'LOG')]
57+
[string]$Type,
58+
59+
60+
# The Share
61+
[Parameter(Mandatory=$true,HelpMessage='This is the root of the backup directory',
62+
ValueFromPipeline=$true,
63+
ValueFromPipelineByPropertyName=$true,
64+
ValueFromRemainingArguments=$false,
65+
Position=2)]
66+
[ValidateNotNull()]
67+
[ValidateNotNullOrEmpty()]
68+
[String]$Share,
69+
70+
#If you wish to see the results of the query
71+
[switch]$OutputResults,
72+
73+
# Compression off ?
74+
[switch]$CompressionOff,
75+
76+
# Copy Only ?
77+
[switch]$CopyOnly
78+
79+
)
80+
81+
82+
DynamicParam {
83+
# Set the dynamic parameters' name
84+
$ParameterName = 'Database'
85+
86+
# Create the dictionary
87+
$RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
88+
89+
# Create the collection of attributes
90+
$AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
91+
92+
# Create and set the parameters' attributes
93+
$ParameterAttribute = New-Object -TypeName System.Management.Automation.ParameterAttribute
94+
$ParameterAttribute.Mandatory = $true
95+
$ParameterAttribute.Position = 1
96+
97+
# Add the attributes to the attributes collection
98+
$AttributeCollection.Add($ParameterAttribute)
99+
100+
# Generate and set the ValidateSet
101+
# Load the assembly
102+
[void][reflection.assembly]::LoadWithPartialName( 'Microsoft.SqlServer.Smo' )
103+
104+
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Instance
105+
$arrSet = $Srv.databases.Name
106+
$srv.ConnectionContext.Disconnect()
107+
$ValidateSetAttribute = New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList ($arrSet)
108+
109+
# Add the ValidateSet to the attributes collection
110+
$AttributeCollection.Add($ValidateSetAttribute)
111+
112+
# Create and return the dynamic parameter
113+
$RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList ($ParameterName, [string], $AttributeCollection)
114+
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
115+
return $RuntimeParameterDictionary
116+
}
117+
118+
begin {
119+
# Bind the parameter to a friendly variable
120+
$Database = $PsBoundParameters[$ParameterName]
121+
122+
if($CompressionOff)
123+
{
124+
$Compression = 'N'
125+
}
126+
else
127+
{
128+
$Compression = 'Y'
129+
}
130+
if($CopyOnly)
131+
{
132+
$Co = 'Y'
133+
}
134+
else
135+
{
136+
$CO = 'N'
137+
}
138+
}
139+
Process
140+
{
141+
142+
$Query = @"
143+
EXECUTE [master].[dbo].[DatabaseBackup]
144+
@Databases = '$Database',
145+
@Directory = N'$Share',
146+
@BackupType = '$Type',
147+
@Verify = 'Y',
148+
@CheckSum = 'Y',
149+
@Compress= '$Compression',
150+
@LogToTable = 'Y',
151+
@CopyOnly = '$CO'
152+
"@
153+
154+
if ($pscmdlet.ShouldProcess("$Instance" , "Back up $Database"))
155+
{
156+
try
157+
{
158+
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Instance
159+
$SqlConnection = $srv.ConnectionContext
160+
if($OutputResults)
161+
{
162+
Register-ObjectEvent -InputObject $SqlConnection -EventName InfoMessage -SupportEvent -Action {
163+
Write-Host $Event.SourceEventArgs
164+
}
165+
}
166+
$SqlConnection.StatementTimeout = 8000
167+
$SqlConnection.ConnectTimeout = 10
168+
$SqlConnection.Connect()
169+
$SqlConnection.ExecuteNonQuery($Query)
170+
Write-Output -InputObject "$Database has been backed up to $Share"
171+
}
172+
catch
173+
{
174+
Write-Warning -Message ("Something went Wrong. Run `$Error[0] | Fl -force to work out why")
175+
$Query
176+
}
177+
}
178+
}
179+
End
180+
{
181+
$srv.ConnectionContext.Disconnect()
182+
}
183+
}

Snippets List.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## A list of snippets
2+
3+
# Create a new snippet snippet!
4+
5+
$snippet1 = @{
6+
Title = 'New-Snippet'
7+
Description = 'Create a New Snippet'
8+
Text = @"
9+
`$snippet = @{
10+
Title = `'Put Title Here`'
11+
Description = `'Description Here`'
12+
Text = @`"
13+
Code in Here
14+
`"@
15+
}
16+
New-IseSnippet @snippet
17+
"@
18+
}
19+
New-IseSnippet @snippet1 –Force
20+
21+
# SMO Snippet
22+
23+
$snippet = @{
24+
Title = 'SMO-Server'
25+
Description = 'Creates a SQL Server SMO Object'
26+
Text = @"
27+
`$srv = New-Object Microsoft.SqlServer.Management.Smo.Server `$Server
28+
"@
29+
}
30+
New-IseSnippet @snippet
31+
32+
## Data table snippet
33+
34+
$snippet = @{
35+
Title = 'New-DataTable'
36+
Description = 'Creates a Data Table Object'
37+
Text = @"
38+
# Create Table Object
39+
`$table = New-Object system.Data.DataTable `$TableName
40+
41+
# Create Columns
42+
`$col1 = New-Object system.Data.DataColumn NAME1,([string])
43+
`$col2 = New-Object system.Data.DataColumn NAME2,([decimal])
44+
45+
#Add the Columns to the table
46+
`$table.columns.add(`$col1)
47+
`$table.columns.add(`$col2)
48+
49+
# Create a new Row
50+
`$row = `$table.NewRow()
51+
52+
# Add values to new row
53+
`$row.Name1 = 'VALUE'
54+
`$row.NAME2 = 'VALUE'
55+
56+
#Add new row to table
57+
`$table.Rows.Add(`$row)
58+
"@
59+
}
60+
New-IseSnippet @snippet
61+
#formatted duration snippet
62+
$snippet = @{
63+
Title = 'Formatted Duration'
64+
Description = 'Formats Get-SQLAgentJobHistory into timespan'
65+
Text = @"
66+
`$FormattedDuration = @{
67+
Name = 'FormattedDuration'
68+
Expression = {
69+
[timespan]`$_.RunDuration.ToString().PadLeft(6,'0').insert(4,':').insert(2,':')
70+
}
71+
}
72+
"@
73+
}
74+
New-IseSnippet @snippet

0 commit comments

Comments
 (0)