Skip to content

Commit b46de8d

Browse files
committed
Merge branch 'development' into 'master'
Development to Master Closes #6 and #10 See merge request sanderstad/PSDatabaseClone!3
2 parents 9622463 + a6fbdd3 commit b46de8d

16 files changed

+807
-400
lines changed

PSDatabasClone.psd1 renamed to PSDatabaseClone.psd1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
# Script module or binary module file associated with this manifest
3-
ModuleToProcess = 'PSDatabasClone.psm1'
3+
ModuleToProcess = 'PSDatabaseClone.psm1'
44

55
# Version number of this module.
66
ModuleVersion = '1.0.0.0'
@@ -63,14 +63,15 @@
6363
# NestedModules = @()
6464

6565
# Functions to export from this module
66-
FunctionsToExport = 'Initialize-PdcVhdDisk',
67-
'Invoke-PdcRepairClone',
68-
'New-PdcDatabaseClone',
69-
'New-PdcDatabaseImage',
70-
'New-PdcVhdDisk',
71-
'Remove-PdcDatabaseClone',
72-
'Remove-PdcDatabaseImage',
73-
'Set-PdcConfiguration'
66+
FunctionsToExport = 'Initialize-PDCVhdDisk',
67+
'Invoke-PDCRepairClone',
68+
'New-PDCClone',
69+
'New-PDCImage',
70+
'New-PDCVhdDisk',
71+
'Remove-PDCClone',
72+
'Remove-PDCImage',
73+
'Set-PDCConfiguration',
74+
'Get-PDCClone'
7475

7576
# Cmdlets to export from this module
7677
CmdletsToExport = ''

PSDatabasClone.psm1 renamed to PSDatabaseClone.psm1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ function Import-ModuleFile
66
<#
77
.SYNOPSIS
88
Loads files into the module on module import.
9-
9+
1010
.DESCRIPTION
1111
This helper function is used during module initialization.
1212
It should always be dotsourced itself, in order to proper function.
13-
13+
1414
This provides a central location to react to files being imported, if later desired
15-
15+
1616
.PARAMETER Path
1717
The path to the file to load
18-
18+
1919
.EXAMPLE
2020
PS C:\> . Import-ModuleFile -File $function.FullName
21-
21+
2222
Imports the file stored in $function according to import policy
2323
#>
2424
[CmdletBinding()]
2525
Param (
2626
[string]
2727
$Path
2828
)
29-
29+
3030
if ($doDotSource) { . $Path }
3131
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null) }
3232
}

functions/Get-PDCClone.ps1

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
function Get-PDCClone {
2+
<#
3+
.SYNOPSIS
4+
Get-PDCClone get on or more clones
5+
6+
.DESCRIPTION
7+
Get-PDCClone will retrieve the clones and apply filters if needed.
8+
By default all the clones are returned
9+
10+
.PARAMETER HostName
11+
Filter based on the hostname
12+
13+
.PARAMETER Database
14+
Filter based on the database
15+
16+
.PARAMETER ImageID
17+
Filter based on the image id
18+
19+
.PARAMETER ImageName
20+
Filter based on the image name
21+
22+
.PARAMETER ImageLocation
23+
Filter based on the image location
24+
25+
.NOTES
26+
Author: Sander Stad (@sqlstad, sqlstad.nl)
27+
28+
Website: https://psdatabaseclone.io
29+
Copyright: (C) Sander Stad, [email protected]
30+
License: MIT https://opensource.org/licenses/MIT
31+
32+
.LINK
33+
https://psdatabaseclone.io/
34+
35+
.EXAMPLE
36+
Get-PDCClone -HostName host1, host2
37+
38+
Retrieve the clones for host1 and host2
39+
40+
.EXAMPLE
41+
Get-PDCClone -Database DB1
42+
43+
Get all the clones that have the name DB1
44+
45+
.EXAMPLE
46+
Get-PDCClone -ImageName DB1_20180703085917
47+
48+
Get all the clones that were made with image "DB1_20180703085917"
49+
#>
50+
51+
[CmdLetBinding()]
52+
53+
param(
54+
[string[]]$HostName,
55+
[string[]]$Database,
56+
[int[]]$ImageID,
57+
[string[]]$ImageName,
58+
[string[]]$ImageLocation
59+
)
60+
61+
begin {
62+
63+
# Test the module database setup
64+
try {
65+
Test-PDCConfiguration -EnableException
66+
}
67+
catch {
68+
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
69+
}
70+
71+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
72+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
73+
74+
}
75+
76+
process {
77+
78+
# Test if there are any errors
79+
if (Test-PSFFunctionInterrupt) { return }
80+
81+
$query = "
82+
SELECT c.CloneID,
83+
c.CloneLocation,
84+
c.AccessPath,
85+
c.SqlInstance,
86+
c.DatabaseName,
87+
c.IsEnabled,
88+
i.ImageID,
89+
i.ImageName,
90+
i.ImageLocation,
91+
h.HostName
92+
FROM dbo.Clone AS c
93+
INNER JOIN dbo.Host AS h
94+
ON h.HostID = c.HostID
95+
INNER JOIN dbo.Image AS i
96+
ON i.ImageID = c.ImageID;
97+
"
98+
99+
try {
100+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject
101+
}
102+
catch {
103+
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query
104+
}
105+
106+
# Filter host name
107+
if($HostName){
108+
$results = $results | Where-Object {$_.HostName -in $HostName}
109+
}
110+
111+
# Filter image id
112+
if($Database){
113+
$results = $results | Where-Object {$_.DatabaseName -in $Database}
114+
}
115+
116+
# Filter image id
117+
if($ImageID){
118+
$results = $results | Where-Object {$_.ImageID -in $ImageID}
119+
}
120+
121+
# Filter image name
122+
if($ImageName){
123+
$results = $results | Where-Object {$_.ImageName -in $ImageName}
124+
}
125+
126+
# Filter image location
127+
if($ImageLocation){
128+
$results = $results | Where-Object {$_.ImageLocation -in $ImageLocation}
129+
}
130+
131+
return $results
132+
}
133+
134+
end {
135+
136+
# Test if there are any errors
137+
if (Test-PSFFunctionInterrupt) { return }
138+
139+
Write-PSFMessage -Message "Finished retrieving clone(s)" -Level Verbose
140+
141+
}
142+
143+
}

functions/Initialize-PdcVhdDisk.ps1 renamed to functions/Initialize-PDCVhdDisk.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function Initialize-PdcVhdDisk {
1+
function Initialize-PDCVhdDisk {
22
<#
33
.SYNOPSIS
4-
Initialize-PdcVhdDisk initialized the VHD
4+
Initialize-PDCVhdDisk initialized the VHD
55
66
.DESCRIPTION
7-
Initialize-PdcVhdDisk will initialize the VHD.
7+
Initialize-PDCVhdDisk will initialize the VHD.
88
It mounts the disk, creates a volume, creates the partition and sets it to active
99
1010
.PARAMETER Path
@@ -15,6 +15,17 @@ function Initialize-PdcVhdDisk {
1515
1616
$scred = Get-Credential, then pass $scred object to the -Credential parameter.
1717
18+
.PARAMETER EnableException
19+
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
20+
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
21+
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
22+
23+
.PARAMETER WhatIf
24+
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
25+
26+
.PARAMETER Confirm
27+
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
28+
1829
.NOTES
1930
Author: Sander Stad (@sqlstad, sqlstad.nl)
2031
@@ -26,12 +37,12 @@ function Initialize-PdcVhdDisk {
2637
https://psdatabaseclone.io/
2738
2839
.EXAMPLE
29-
Initialize-PdcVhdDisk -Path $path
40+
Initialize-PDCVhdDisk -Path $path
3041
3142
Initialize the disk pointing to the path with all default settings
3243
3344
.EXAMPLE
34-
Initialize-PdcVhdDisk -Path $path -AllocationUnitSize 4KB
45+
Initialize-PDCVhdDisk -Path $path -AllocationUnitSize 4KB
3546
3647
Initialize the disk and format the partition with a 4Kb allocation unit size
3748
@@ -46,7 +57,8 @@ function Initialize-PdcVhdDisk {
4657
$Credential,
4758
[ValidateSet('GPT', 'MBR')]
4859
[string]$PartitionStyle = 'GPT',
49-
[int]$AllocationUnitSize = 64KB
60+
[int]$AllocationUnitSize = 64KB,
61+
[switch]$EnableException
5062
)
5163

5264
begin {
@@ -106,7 +118,6 @@ function Initialize-PdcVhdDisk {
106118

107119
# Add the results to the custom object
108120
[PSCustomObject]@{
109-
DiskNumber = $disk.DiskNumber
110121
Disk = $disk
111122
Partition = (Get-Partition -Disk $disk)
112123
Volume = $volume

functions/Invoke-PdcRepairClone.ps1 renamed to functions/Invoke-PDCRepairClone.ps1

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function Invoke-PdcRepairClone {
1+
function Invoke-PDCRepairClone {
22
<#
33
.SYNOPSIS
4-
Invoke-PdcRepairClone repairs the clones
4+
Invoke-PDCRepairClone repairs the clones
55
66
.DESCRIPTION
7-
Invoke-PdcRepairClone has the ability to repair the clones when they have gotten disconnected from the image.
7+
Invoke-PDCRepairClone has the ability to repair the clones when they have gotten disconnected from the image.
88
In such a case the clone is no longer available for the database server and the database will either not show
99
any information or the database will have the status (Recovery Pending).
1010
@@ -21,6 +21,17 @@ function Invoke-PdcRepairClone {
2121
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
2222
To connect as a different Windows user, run PowerShell as that user.
2323
24+
.PARAMETER EnableException
25+
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
26+
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
27+
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
28+
29+
.PARAMETER WhatIf
30+
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
31+
32+
.PARAMETER Confirm
33+
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
34+
2435
.NOTES
2536
Author: Sander Stad (@sqlstad, sqlstad.nl)
2637
@@ -32,7 +43,7 @@ function Invoke-PdcRepairClone {
3243
https://psdatabaseclone.io/
3344
3445
.EXAMPLE
35-
Invoke-PdcRepairClone -Hostname Host1
46+
Invoke-PDCRepairClone -Hostname Host1
3647
3748
Repair the clones for Host1
3849
@@ -42,20 +53,23 @@ function Invoke-PdcRepairClone {
4253
param(
4354
[Parameter(Mandatory = $true)]
4455
[string[]]$HostName,
45-
4656
[System.Management.Automation.PSCredential]
47-
$SqlCredential
57+
$SqlCredential,
58+
[switch]$EnableException
4859
)
4960

5061
begin {
5162
# Test the module database setup
52-
$result = Test-PdcConfiguration
53-
54-
if(-not $result.Check){
55-
Stop-PSFFunction -Message $result.Message -Target $result -Continue
56-
return
63+
try {
64+
Test-PDCConfiguration -EnableException
65+
}
66+
catch {
67+
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
5768
}
5869

70+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
71+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
72+
5973
}
6074

6175
process {
@@ -84,10 +98,10 @@ function Invoke-PdcRepairClone {
8498
# Get the clones registered for the host
8599
try {
86100
Write-PSFMessage -Message "Get the clones for host $hst" -Level Verbose
87-
$results = Invoke-DbaSqlQuery -SqlInstance $ecDatabaseServer -Database $ecDatabaseName -Query $query
101+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
88102
}
89103
catch {
90-
Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $ecDatabaseServer -ErrorRecord $_ -Continue
104+
Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue
91105
}
92106

93107
# Loop through the results

0 commit comments

Comments
 (0)