Skip to content

Commit 6330379

Browse files
committed
Merge branch 'development' into 'master'
Development See merge request sanderstad/PSDatabaseClone!6
2 parents 9d9bcb5 + 09e37ab commit 6330379

18 files changed

+996
-725
lines changed

PSDatabaseClone.psd1

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

55
# Version number of this module.
6-
ModuleVersion = '0.1.0.0'
6+
ModuleVersion = '0.1.0.0'
77

88
# ID used to uniquely identify this module
9-
GUID = '747980ca-0d3d-4529-b4fd-e6cdd059c62a'
9+
GUID = '747980ca-0d3d-4529-b4fd-e6cdd059c62a'
1010

1111
# Author of this module
12-
Author = 'Sander Stad'
12+
Author = 'Sander Stad'
1313

1414
# Company or vendor of this module
15-
CompanyName = ''
15+
CompanyName = ''
1616

1717
# Copyright statement for this module
18-
Copyright = 'Copyright (c) 2018 Sander Stad'
18+
Copyright = 'Copyright (c) 2018 Sander Stad'
1919

2020
# Description of the functionality provided by this module
21-
Description = 'Cloning module for SQL Server databases'
21+
Description = 'Module for cloning SQL Server databases'
2222

2323
# Minimum version of the Windows PowerShell engine required by this module
24-
PowerShellVersion = '5.0'
24+
PowerShellVersion = '5.0'
2525

2626
# Name of the Windows PowerShell host required by this module
2727
# PowerShellHostName = ''
@@ -40,7 +40,7 @@
4040

4141
# Modules that must be imported into the global environment prior to importing
4242
# this module
43-
RequiredModules = @(
43+
RequiredModules = @(
4444
@{ ModuleName = 'PSFramework'; ModuleVersion = '0.9.10.23' },
4545
@{ ModuleName = 'dbatools'; ModuleVersion = '0.9.337' }
4646
)
@@ -63,34 +63,34 @@
6363
# NestedModules = @()
6464

6565
# Functions to export from this module
66-
FunctionsToExport = 'Initialize-PSDCVhdDisk',
66+
FunctionsToExport =
6767
'Invoke-PSDCRepairClone',
6868
'New-PSDCClone',
6969
'New-PSDCImage',
70-
'New-PSDCVhdDisk',
7170
'Remove-PSDCClone',
72-
'Remove-PSDCImage',
71+
'Remove-PSDCImage',
7372
'Set-PSDCConfiguration',
7473
'Get-PSDCClone',
75-
'Get-PSDCImage'
74+
'Get-PSDCImage',
75+
'Convert-PSDCLocalUncPathToLocalPath'
7676

7777
# Cmdlets to export from this module
78-
CmdletsToExport = ''
78+
CmdletsToExport = ''
7979

8080
# Variables to export from this module
81-
VariablesToExport = ''
81+
VariablesToExport = ''
8282

8383
# Aliases to export from this module
84-
AliasesToExport = ''
84+
AliasesToExport = ''
8585

8686
# List of all modules packaged with this module
87-
ModuleList = @()
87+
ModuleList = @()
8888

8989
# List of all files packaged with this module
90-
FileList = @()
90+
FileList = @()
9191

9292
# Private data to pass to the module specified in ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
93-
PrivateData = @{
93+
PrivateData = @{
9494

9595
#Support for PowerShellGet galleries.
9696
PSData = @{

en-us/about_PSDatabaseClone.help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ LONG DESCRIPTION
1010

1111
KEYWORDS
1212
PSDatabaseClone
13+
SQL Server
14+
Database

functions/Get-PSDCClone.ps1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,24 @@
7575

7676
begin {
7777

78+
# Get the module configurations
79+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
80+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
81+
if (-not $pdcCredential) {
82+
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
83+
}
84+
else {
85+
$pdcCredential = $PSDCSqlCredential
86+
}
87+
7888
# Test the module database setup
7989
try {
80-
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
90+
Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException
8191
}
8292
catch {
8393
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
8494
}
8595

86-
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
87-
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
88-
8996
$query = "
9097
SELECT c.CloneID,
9198
c.CloneLocation,
@@ -106,7 +113,7 @@
106113

107114
try {
108115
$results = @()
109-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -As PSObject
116+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query -As PSObject
110117
}
111118
catch {
112119
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query

functions/Get-PSDCImage.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,24 @@
7070
)
7171

7272
begin {
73+
# Get the module configurations
74+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
75+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
76+
if (-not $pdcCredential) {
77+
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
78+
}
79+
else {
80+
$pdcCredential = $PSDCSqlCredential
81+
}
82+
7383
# Test the module database setup
7484
try {
75-
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
85+
Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException
7686
}
7787
catch {
7888
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
7989
}
8090

81-
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
82-
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
83-
8491
$query = "
8592
SELECT ImageID,
8693
ImageName,

functions/Invoke-PSDCRepairClone.ps1

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
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 Credential
25+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
26+
This works similar as SqlCredential but is only meant for authentication to the the host
27+
2428
.PARAMETER PSDCSqlCredential
2529
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
2630
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
@@ -53,30 +57,39 @@
5357
5458
#>
5559

56-
[CmdLetBinding()]
60+
[CmdLetBinding(SupportsShouldProcess = $true)]
5761

5862
param(
5963
[Parameter(Mandatory = $true)]
6064
[string[]]$HostName,
6165
[System.Management.Automation.PSCredential]
6266
$SqlCredential,
6367
[System.Management.Automation.PSCredential]
68+
$Credential,
69+
[System.Management.Automation.PSCredential]
6470
$PSDCSqlCredential,
6571
[switch]$EnableException
6672
)
6773

6874
begin {
75+
# Get the module configurations
76+
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server
77+
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
78+
if (-not $pdcCredential) {
79+
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
80+
}
81+
else {
82+
$pdcCredential = $PSDCSqlCredential
83+
}
84+
6985
# Test the module database setup
7086
try {
71-
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
87+
Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException
7288
}
7389
catch {
7490
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
7591
}
7692

77-
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
78-
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
79-
8093
}
8194

8295
process {
@@ -86,6 +99,21 @@
8699
# Loop through each of the hosts
87100
foreach ($hst in $HostName) {
88101

102+
# Setup the computer object
103+
$computer = [PsfComputer]$hst
104+
105+
if (-not $computer.IsLocalhost) {
106+
$command = [scriptblock]::Create("Import-Module PSDatabaseClone")
107+
108+
try {
109+
Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
110+
}
111+
catch {
112+
Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command
113+
return
114+
}
115+
}
116+
89117
$query = "
90118
SELECT i.ImageLocation,
91119
c.CloneLocation,
@@ -105,7 +133,7 @@
105133
# Get the clones registered for the host
106134
try {
107135
Write-PSFMessage -Message "Get the clones for host $hst" -Level Verbose
108-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query
136+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query
109137
}
110138
catch {
111139
Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue
@@ -125,7 +153,16 @@
125153
try {
126154
Write-PSFMessage -Message "Mounting vhd $($result.CloneLocation)" -Level Verbose
127155

128-
Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue
156+
# Check if computer is local
157+
if ($PSCmdlet.ShouldProcess($result.CloneLocation, "Mounting $($result.CloneLocation)")) {
158+
if ($computer.IsLocalhost) {
159+
$null = Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue
160+
}
161+
else {
162+
$command = [ScriptBlock]::Create("Mount-VHD -Path $($result.CloneLocation) -NoDriveLetter -ErrorAction SilentlyContinue")
163+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
164+
}
165+
}
129166
}
130167
catch {
131168
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $clone -Continue
@@ -139,7 +176,17 @@
139176
if ($result.DatabaseName -notin $databases.Name) {
140177

141178
# Get all the files of the database
142-
$databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer}
179+
if ($PSCmdlet.ShouldProcess($result.AccessPath, "Retrieving database files from $($result.AccessPath)")) {
180+
# Check if computer is local
181+
if ($computer.IsLocalhost) {
182+
$databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer}
183+
}
184+
else {
185+
$commandText = "Get-ChildItem -Path $($result.AccessPath) -Recurse | " + 'Where-Object {-not $_.PSIsContainer}'
186+
$command = [ScriptBlock]::Create($commandText)
187+
$databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
188+
}
189+
}
143190

144191
# Setup the database filestructure
145192
$dbFileStructure = New-Object System.Collections.Specialized.StringCollection
@@ -152,7 +199,14 @@
152199
Write-PSFMessage -Message "Mounting database from clone" -Level Verbose
153200

154201
# Mount the database using the config file
155-
$null = Mount-DbaDatabase -SqlInstance $result.SQLInstance -Database $result.DatabaseName -FileStructure $dbFileStructure
202+
if ($PSCmdlet.ShouldProcess($result.DatabaseName, "Mounting database $($result.DatabaseName) to $($result.SQLInstance)")) {
203+
try {
204+
$null = Mount-DbaDatabase -SqlInstance $result.SQLInstance -Database $result.DatabaseName -FileStructure $dbFileStructure
205+
}
206+
catch {
207+
Stop-PSFFunction -Message "Couldn't mount database $($result.DatabaseName)" -Target $result.DatabaseName -Continue
208+
}
209+
}
156210
}
157211
else {
158212
Write-PSFMessage -Message "Database $($result.Database) is already attached" -Level Verbose

0 commit comments

Comments
 (0)