|
15 | 15 | .PARAMETER FileName |
16 | 16 | The file name of the VHD |
17 | 17 |
|
| 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 | +
|
18 | 22 | .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 |
20 | 26 |
|
21 | 27 | .PARAMETER FixedSize |
22 | 28 | Set the VHD to have a fixed size or not. |
|
63 | 69 | [string]$Destination, |
64 | 70 | [string]$Name, |
65 | 71 | [string]$FileName, |
66 | | - [uint64]$Size = 64TB, |
| 72 | + [ValidateSet('VHD', 'VHDX')] |
| 73 | + [string]$VhdType, |
| 74 | + [uint64]$Size, |
67 | 75 | [switch]$FixedSize, |
68 | 76 | [switch]$ReadOnly, |
69 | 77 | [switch]$Force, |
|
84 | 92 | } |
85 | 93 | } |
86 | 94 |
|
| 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 | + |
87 | 124 | # Check the name and file name parameters |
88 | 125 | if (-not $Name -and -not $FileName) { |
89 | 126 | Stop-PSFFunction -Message "Either set the Name or FileName parameter" |
90 | 127 | } |
91 | 128 | else { |
92 | 129 | if (-not $FileName) { |
93 | | - $FileName = "$Name.vhdx" |
| 130 | + $FileName = "$Name.$($VhdType.ToLower())" |
94 | 131 | Write-PSFMessage -Message "Setting file name to $FileName" -Level Verbose |
95 | 132 | } |
96 | 133 | elseif ($FileName) { |
|
112 | 149 |
|
113 | 150 | # Check if the file does not yet exist |
114 | 151 | 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 | + } |
116 | 163 | } |
117 | 164 |
|
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" |
122 | 167 | } |
123 | 168 |
|
124 | 169 | process { |
|
129 | 174 | # Check if the file needs to have a fixed size |
130 | 175 | try { |
131 | 176 | if ($FixedSize) { |
132 | | - $null = New-VHD -Path $vhdPath -SizeBytes $Size -Fixed |
| 177 | + $command = "create vdisk file='$vhdPath' maximum=$Size type=fixed" |
133 | 178 | } |
134 | 179 | else { |
135 | | - $null = New-VHD -Path $vhdPath -SizeBytes $Size -Dynamic |
| 180 | + $command = "create vdisk file='$vhdPath' maximum=$Size type=expandable" |
136 | 181 | } |
| 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 | + |
137 | 189 | } |
138 | 190 | catch { |
139 | 191 | Stop-PSFFunction -Message "Something went wrong creating the vhd" -ErrorRecord $_ -Continue |
|
142 | 194 | } |
143 | 195 |
|
144 | 196 | end { |
| 197 | + # Clean up the script file for diskpart |
| 198 | + Remove-Item $diskpartScriptFile -Force |
| 199 | + |
145 | 200 | # Test if there are any errors |
146 | 201 | if (Test-PSFFunctionInterrupt) { return } |
147 | 202 |
|
|
0 commit comments