Skip to content

Commit 3e5c628

Browse files
committed
Added parameter for Vhd type. Added check for type ad size and lots more
1 parent b1c0860 commit 3e5c628

1 file changed

Lines changed: 65 additions & 10 deletions

File tree

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 = "$($MyInvocation.MyCommand.Module.ModuleBase)\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)