Skip to content

Commit fe0a623

Browse files
authored
Merge pull request #9 from sanderstad/development
Migrate away from Hyper-V
2 parents ec611f6 + 313f861 commit fe0a623

File tree

13 files changed

+206
-68
lines changed

13 files changed

+206
-68
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
5+
---
6+
7+
**Before submitting the bug**
8+
- [ ] Ensure you are able to reproduce it on the latest released version (_we release often_)
9+
- [ ] Verified this bug is not already reported in an issue
10+
- [ ] Verified errors are not related to permissions
11+
- [ ] Can reproduce in a clean PowerShell session (_clean = `powershell -NoProfile`_)
12+
13+
**Describe the bug**
14+
A clear and concise description of what the bug is.
15+
16+
**To Reproduce**
17+
Steps to reproduce the behavior:
18+
```powershell
19+
<#
20+
Provide exact (or sanitized) code to reproduce the error
21+
#>
22+
```
23+
24+
**Expected behavior**
25+
A clear and concise description of what you expected to happen.
26+
27+
**Screenshots**
28+
If applicable, add screenshots to help explain your problem.
29+
30+
**Environmental data**
31+
<!-- Provide output of the following two commands -->
32+
33+
- PowerShell: <!-- $PSVersionTable output -->
34+
35+
**Additional context**
36+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
5+
---
6+
7+
**Is your feature request related to a problem? Please describe.**
8+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
9+
10+
**Describe the solution you'd like**
11+
A clear and concise description of what you want to happen.
12+
13+
**Describe alternatives you've considered**
14+
A clear and concise description of any alternative solutions or features you've considered.
15+
16+
**Latest version of dbatools as of writing**
17+
Please provide the latest released version of dbatools module.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

PSDatabaseClone.psd1

Lines changed: 1 addition & 1 deletion
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 = 'PSDatabaseClone.psm1'
3+
RootModule = 'PSDatabaseClone.psm1'
44

55
# Version number of this module.
66
ModuleVersion = '0.1.47'

appveyor.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# See http://www.appveyor.com/docs/appveyor-yml for many more options
2-
version: 0.1.{build}
2+
version: 0.2.{build}
33

44
cache:
55
- C:\ProgramData\chocolatey\bin -> appveyor.yml
@@ -13,7 +13,7 @@ shallow_clone: true
1313

1414
environment:
1515
environment: development
16-
version: 0.1.$(appveyor_build_number)
16+
version: 0.2.$(appveyor_build_number)
1717
appveyor_rdp_password: Psd@tabaseclone2018
1818

1919
matrix:
@@ -28,4 +28,4 @@ test_script:
2828
- ps: .\tests\pester.ps1
2929

3030
#on_finish:
31-
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
31+
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

bin/PSDatabaseClone.dll

10 KB
Binary file not shown.

functions/clone/Get-PSDCClone.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@
207207
HostName = $result.HostName
208208
}
209209
}
210-
211210
}
212211

213212
end {

functions/clone/New-PSDCClone.ps1

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
)
111111

112112
begin {
113+
# Check if the setup has ran
114+
if (-not (Get-PSFConfigValue -FullName psdatabaseclone.setup.status)) {
115+
Stop-PSFFunction -Message "The module setup has NOT yet successfully run. Please run 'Set-PSDCConfiguration'"
116+
return
117+
}
118+
113119
# Get the information store
114120
$informationStore = Get-PSFConfigValue -FullName psdatabaseclone.informationstore.mode
115121

@@ -145,6 +151,9 @@
145151
if ($Disabled) {
146152
$active = 0
147153
}
154+
155+
# Set the location where to save the diskpart command
156+
$diskpartScriptFile = Get-PSFConfigValue -FullName psdatabaseclone.diskpart.scriptfile -Fallback "$env:APPDATA\psdatabaseclone\diskpartcommand.txt"
148157
}
149158

150159
process {
@@ -161,12 +170,6 @@
161170

162171
# Setup the computer object
163172
$computer = [PsfComputer]$server.Name
164-
165-
# Check if Hyper-V enabled for the SQL instance
166-
if (-not (Test-PSDCHyperVEnabled -HostName $computer.ComputerName -Credential $Credential)) {
167-
Stop-PSFFunction -Message "Hyper-V is not enabled on host." -ErrorRecord $_ -Target $computer -Continue
168-
return
169-
}
170173
}
171174
catch {
172175
Stop-PSFFunction -Message "Could not connect to Sql Server instance $instance" -ErrorRecord $_ -Target $instance
@@ -357,9 +360,15 @@
357360
try {
358361
Write-PSFMessage -Message "Creating clone from $ParentVhd" -Level Verbose
359362

363+
$command = "create vdisk file='$Destination\$CloneName.vhdx' parent='$ParentVhd'"
364+
360365
# Check if computer is local
361366
if ($computer.IsLocalhost) {
362-
$vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing
367+
# Set the content of the diskpart script file
368+
Set-Content -Path $diskpartScriptFile -Value $command -Force
369+
370+
$script = [ScriptBlock]::Create("diskpart /s $diskpartScriptFile")
371+
$null = Invoke-PSFCommand -ScriptBlock $script
363372
}
364373
else {
365374
$command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path `"$Destination\$CloneName.vhdx`" -Differencing")
@@ -372,7 +381,7 @@
372381

373382
}
374383
catch {
375-
Stop-PSFFunction -Message "Could not create clone" -Target $vhd -Continue
384+
Stop-PSFFunction -Message "Could not create clone" -Target $vhd -Continue -ErrorRecord $_
376385
}
377386
}
378387

@@ -384,14 +393,14 @@
384393
# Check if computer is local
385394
if ($computer.IsLocalhost) {
386395
# Mount the disk
387-
$null = Mount-VHD -Path "$Destination\$CloneName.vhdx" -NoDriveLetter
396+
$null = Mount-DiskImage -ImagePath "$Destination\$CloneName.vhdx"
388397

389398
# Get the disk based on the name of the vhd
390399
$disk = Get-Disk | Where-Object {$_.Location -eq "$Destination\$CloneName.vhdx"}
391400
}
392401
else {
393402
# Mount the disk
394-
$command = [ScriptBlock]::Create("Mount-VHD -Path `"$Destination\$CloneName.vhdx`" -NoDriveLetter")
403+
$command = [ScriptBlock]::Create("Mount-DiskImage -ImagePath `"$Destination\$CloneName.vhdx`"")
395404
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
396405

397406
# Get the disk based on the name of the vhd

functions/clone/Remove-PSDCClone.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@
224224
try {
225225

226226
if ($computer.IsLocalhost) {
227-
$null = Dismount-VHD -Path $item.CloneLocation -Credential $Credential
227+
$null = Dismount-DiskImage -ImagePath $item.CloneLocation
228228
}
229229
else {
230230
$command = [ScriptBlock]::Create("Test-Path -Path '$($item.CloneLocation)'")
231231
Write-PSFMessage -Message "Dismounting disk '$($item.CloneLocation)' from $($item.HostName)" -Level Verbose
232232
$result = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
233233
#if (-not $result) {
234-
$command = [scriptblock]::Create("Dismount-VHD -Path '$($item.CloneLocation)'")
234+
$command = [scriptblock]::Create("Dismount-DiskImage -ImagePath '$($item.CloneLocation)'")
235235
$null = Invoke-PSFCommand -ComputerName $item.HostName -ScriptBlock $command -Credential $Credential
236236
#}
237237
}

functions/disk/Initialize-PSDCVhdDisk.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@
8787
Write-PSFMessage -Message "Vhd is already mounted" -Level Warning
8888

8989
# retrieve the specific disk
90-
$disk = $disks | Where-Object {$_.Location -eq $Path}
90+
$disk = $disks | Where-Object Location -eq $Path
9191
}
9292
else {
93-
if ($PSCmdlet.ShouldProcess($disk, "Mounting disk")) {
93+
if ($PSCmdlet.ShouldProcess("Mounting disk")) {
9494
# Mount the vhd
9595
try {
9696
Write-PSFMessage -Message "Mounting disk $disk" -Level Verbose
9797

98-
$disk = Mount-VHD -Path $Path -PassThru | Get-Disk
98+
# Mount the disk
99+
Mount-DiskImage -ImagePath $Path
100+
101+
# Get the disk
102+
$disk = Get-Disk | Where-Object Location -eq $Path
99103
}
100104
catch {
101105
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $Path -ErrorRecord $_ -Continue
@@ -123,7 +127,7 @@
123127
}
124128
catch {
125129
# Dismount the drive
126-
Dismount-VHD -Path $Path
130+
Dismount-DiskImage -DiskImage $Path
127131

128132
Stop-PSFFunction -Message "Couldn't create the partition" -Target $disk -ErrorRecord $_ -Continue
129133
}

functions/disk/New-PSDCVhdDisk.ps1

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
.PARAMETER FileName
1616
The file name of the VHD
1717
18+
.PARAMETER VhdType
19+
The type of the harddisk. This can either by VHD (version 1) or VHDX (version 2)
20+
The default is VHDX.
21+
1822
.PARAMETER Size
19-
The size of the VHD.
23+
The size of the VHD in MB.
24+
If no size is used the default will be set to the type of VHD.
25+
The default for VHD is 2 TB and for VHDX 64TB
2026
2127
.PARAMETER FixedSize
2228
Set the VHD to have a fixed size or not.
@@ -63,7 +69,9 @@
6369
[string]$Destination,
6470
[string]$Name,
6571
[string]$FileName,
66-
[uint64]$Size = 64TB,
72+
[ValidateSet('VHD', 'VHDX')]
73+
[string]$VhdType,
74+
[uint64]$Size,
6775
[switch]$FixedSize,
6876
[switch]$ReadOnly,
6977
[switch]$Force,
@@ -84,13 +92,42 @@
8492
}
8593
}
8694

95+
# Check the vhd type
96+
if (-not $VhdType) {
97+
Write-PSFMessage -Message "Setting vhd type to 'VHD" -Level Verbose
98+
$VhdType = 'VHDX'
99+
}
100+
101+
# Check the size of the file
102+
if (-not $Size) {
103+
switch ($VhdType) {
104+
'VHD' { $Size = 2TB }
105+
'VHDX' { $Size = 64TB }
106+
}
107+
108+
# Make sure the size in MB instead of some other version
109+
$Size = $Size / 1MB
110+
}
111+
else {
112+
if ($VhdType -eq 'VHD' -and $size -gt 2TB) {
113+
Stop-PSFFunction -Message "Size cannot exceed 2TB when using VHD type."
114+
}
115+
elseif ($VhdType -eq 'VHDX' -and $size -gt 64TB) {
116+
Stop-PSFFunction -Message "Size cannot exceed 64TB when using VHDX type."
117+
}
118+
119+
if ($Size -lt 3MB) {
120+
Stop-PSFFunction -Message "The size of the vhd cannot be smaller than 3MB" -Continue
121+
}
122+
}
123+
87124
# Check the name and file name parameters
88125
if (-not $Name -and -not $FileName) {
89126
Stop-PSFFunction -Message "Either set the Name or FileName parameter"
90127
}
91128
else {
92129
if (-not $FileName) {
93-
$FileName = "$Name.vhdx"
130+
$FileName = "$Name.$($VhdType.ToLower())"
94131
Write-PSFMessage -Message "Setting file name to $FileName" -Level Verbose
95132
}
96133
elseif ($FileName) {
@@ -112,13 +149,21 @@
112149

113150
# Check if the file does not yet exist
114151
if (Test-Path $vhdPath) {
115-
Stop-PSFFunction -Message "The vhd file already exists" -Continue
152+
if(-not $Force){
153+
Stop-PSFFunction -Message "The vhd file already exists" -Continue
154+
}
155+
else{
156+
try{
157+
Remove-Item -Path $vhdPath -Force:$Force
158+
}
159+
catch{
160+
Stop-PSFFunction -Message "Could not remove VHD '$vhdPath'" -Continue -ErrorRecord $_
161+
}
162+
}
116163
}
117164

118-
# Check the size of the file
119-
if ($Size -lt 3MB) {
120-
Stop-PSFFunction -Message "The size of the vhd cannot be smaller than 3MB" -Continue
121-
}
165+
# Set the location where to save the diskpart command
166+
$diskpartScriptFile = Get-PSFConfigValue -FullName psdatabaseclone.diskpart.scriptfile -Fallback "$env:APPDATA\psdatabaseclone\diskpartcommand.txt"
122167
}
123168

124169
process {
@@ -129,11 +174,18 @@
129174
# Check if the file needs to have a fixed size
130175
try {
131176
if ($FixedSize) {
132-
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Fixed
177+
$command = "create vdisk file='$vhdPath' maximum=$Size type=fixed"
133178
}
134179
else {
135-
$null = New-VHD -Path $vhdPath -SizeBytes $Size -Dynamic
180+
$command = "create vdisk file='$vhdPath' maximum=$Size type=expandable"
136181
}
182+
183+
# Set the content of the diskpart script file
184+
Set-Content -Path $diskpartScriptFile -Value $command -Force
185+
186+
$script = [ScriptBlock]::Create("diskpart /s $diskpartScriptFile")
187+
Invoke-PSFCommand -ScriptBlock $script
188+
137189
}
138190
catch {
139191
Stop-PSFFunction -Message "Something went wrong creating the vhd" -ErrorRecord $_ -Continue
@@ -142,6 +194,9 @@
142194
}
143195

144196
end {
197+
# Clean up the script file for diskpart
198+
Remove-Item $diskpartScriptFile -Force
199+
145200
# Test if there are any errors
146201
if (Test-PSFFunctionInterrupt) { return }
147202

0 commit comments

Comments
 (0)