Public/New-OSDDisk.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
<#
.SYNOPSIS Creates System | OS | Recovery Partitions for MBR or UEFI Drives in WinPE .DESCRIPTION Creates System | OS | Recovery Partitions for MBR or UEFI Drives in WinPE .LINK https://osd.osdeploy.com/module/functions/new-osddisk .NOTES 19.10.10 Created by David Segura @SeguraOSD #> function New-OSDDisk { [CmdletBinding()] param ( #Title displayed during script execution #Default = New-OSDDisk #Alias = T [Alias('T')] [string]$Title = 'New-OSDDisk', #Fixed Disk Number #Alias = Disk, Number [Alias('Disk','Number')] [int]$DiskNumber, #Drive Label of the System Partition #Default = System #Alias = LS, LabelS [Alias('LS','LabelS')] [string]$LabelSystem = 'System', #Drive Label of the Windows Partition #Default = OS #Alias = LW, LabelW [Alias('LW','LabelW')] [string]$LabelWindows = 'OS', #Drive Label of the Recovery Partition #Default = Recovery #Alias = LR, LabelR [Alias('LR','LabelR')] [string]$LabelRecovery = 'Recovery', #System Partition size for BIOS MBR based Computers #Default = 260MB #Range = 100MB - 3000MB (3GB) #Alias = SSM, Mbr, SystemM [Alias('SSM','Mbr','SystemM')] [ValidateRange(100MB,3000MB)] [uint64]$SizeSystemMbr = 260MB, #System Partition size for UEFI GPT based Computers #Default = 260MB #Range = 100MB - 3000MB (3GB) #Alias = SSG, Efi, SystemG [Alias('SSG','Efi','SystemG')] [ValidateRange(100MB,3000MB)] [uint64]$SizeSystemGpt = 260MB, #MSR Partition size #Default = 16MB #Range = 16MB - 128MB #Alias = MSR [Alias('MSR')] [ValidateRange(16MB,128MB)] [uint64]$SizeMSR = 16MB, #Size of the Recovery Partition #Default = 990MB #Range = 350MB - 80000MB (80GB) #Alias = SR, Recovery [Alias('SR','Recovery')] [ValidateRange(350MB,80000MB)] [uint64]$SizeRecovery = 990MB, #Skips the creation of the Recovery Partition [Alias('SkipRecovery')] [switch]$SkipRecoveryPartition, #Required for execution as a safety precaution [switch]$Force ) #====================================================================================================== # Set Defaults #====================================================================================================== $global:OSDDisk = $null $global:OSDFixedDisks = $null $global:OSDDirtyDisks = $null $global:OSDDiskSandbox = $true $global:OSDDiskSelect = $false $OSDDiskSkipDisplay = $false #====================================================================================================== # OSD Module Information #====================================================================================================== $OSDVersion = $($MyInvocation.MyCommand.Module.Version) Write-Host "OSD $OSDVersion $Title" -ForegroundColor Cyan #====================================================================================================== # Get all Fixed Disks #====================================================================================================== $global:OSDFixedDisks = Get-Disk | Where-Object {($_.BusType -ne 'USB') -and ($_.BusType -notmatch 'Virtual') -and ($_.Size -gt 15GB)} | Sort-Object Number #====================================================================================================== # Get all Fixed Disks that are Dirty #====================================================================================================== $global:OSDDirtyDisks = $global:OSDFixedDisks | Where-Object {$_.PartitionStyle -ne 'RAW'} #====================================================================================================== # Failure: No Fixed Disks are present #====================================================================================================== if ($null -eq $global:OSDFixedDisks) {Write-Warning "$Title could not find any Fixed Disks"; Break} #====================================================================================================== # Force Validation #====================================================================================================== if ($Force.IsPresent) {$global:OSDDiskSandbox = $false} #====================================================================================================== # Sandbox #====================================================================================================== if ($global:OSDDiskSandbox -eq $true) { Write-Warning "$Title is running in Sandbox Mode (non-desctructive)" Write-Warning "Use the -Force parameter to bypass Sandbox Mode" } #====================================================================================================== # If there is only one Fixed Disk, then it is the OSDDisk #====================================================================================================== if (($global:OSDFixedDisks | Measure-Object).Count -eq 1) { $OSDDisk = $global:OSDFixedDisks if ($OSDDisk.PartitionStyle -ne 'RAW') { if (-not $Force.IsPresent) { Write-Warning "Existing Partitions must be cleared" } } if ($OSDDisk.PartitionStyle -eq 'RAW') {$OSDDiskSkipDisplay = $true} } else { if (-not $Force.IsPresent) { Write-Warning "Multiple Fixed Disks are present" } if ($DiskNumber) { #OSDDisk was specified $OSDDisk = $global:OSDFixedDisks | Where-Object {$_.DiskNumber -eq $DiskNumber} } else { #More than one Fixed Disk $global:OSDDiskSelect = $true } } #====================================================================================================== # Enable Sandbox #====================================================================================================== #if ($global:OSDDiskSandbox -eq $true) {Write-Verbose "$Title is running in Sandbox. Use the Force parameter for execution" -Verbose} #====================================================================================================== # IsWinPE #====================================================================================================== if (Get-OSDGather -Property IsWinPE) {Write-Host "$Title is running in WinPE" -ForegroundColor DarkGray} else { Write-Warning "$Title requires WinPE" if ($global:OSDDiskSandbox -eq $false) {Break} } #====================================================================================================== # IsAdmin #====================================================================================================== if (Get-OSDGather -Property IsAdmin) {Write-Host "$Title is running with Administrative Rights" -ForegroundColor DarkGray} else { Write-Warning "$Title requires Administrative Rights" if ($global:OSDDiskSandbox -eq $false) {Break} } #====================================================================================================== # DisplayOSDFixedDisks #====================================================================================================== if ($OSDDiskSkipDisplay -eq $false) { Write-Host "=================================================================================================" -ForegroundColor Cyan foreach ($item in $global:OSDFixedDisks) {Write-Host "Disk $($item.Number) - $($item.FriendlyName) ($([math]::Round($item.Size / 1000000000))GB $($item.PartitionStyle)) BusType=$($item.BusType) Partitions=$($item.NumberOfPartitions) IsBoot=$($item.BootFromDisk)" -ForegroundColor Cyan} Write-Host "=================================================================================================" -ForegroundColor Cyan } #====================================================================================================== # OSDDiskSelect #====================================================================================================== if ($global:OSDDiskSelect -eq $true) { do {$ConfirmOSDDisk = Read-Host "Multiple Disks: Type the DiskNumber to use as the OSDDisk, or press X to EXIT (and press Enter)"} until (($global:OSDFixedDisks.Number -Contains $ConfirmOSDDisk) -or $ConfirmOSDDisk -eq 'X') if ($Selected -eq 'X') {Break} $OSDDisk = $global:OSDFixedDisks | Where-Object {$_.Number -eq $ConfirmOSDDisk} } #====================================================================================================== # No GetOSDDisk #====================================================================================================== if ($null -eq $OSDDisk) {Write-Warning "$Title is unable to find a suitable Disk"; Break} #====================================================================================================== # Clear GetOSDDisk #====================================================================================================== if ($OSDDisk.PartitionStyle -ne 'RAW') { #Write-Host "Clear Disk $($OSDDisk.Number) $($OSDDisk.FriendlyName) ($([math]::Round($OSDDisk.Size / 1000000000))GB $($OSDDisk.PartitionStyle)) BusType=$($OSDDisk.BusType) Partitions=$($OSDDisk.NumberOfPartitions)" -ForegroundColor Yellow if ($global:OSDDiskSandbox -eq $true) { Write-Host "SANDBOX: DISKPART select disk $($OSDDisk.Number)" -ForegroundColor DarkGray Write-Host "SANDBOX: DISKPART clean" -ForegroundColor DarkGray } if ($global:OSDDiskSandbox -eq $false) { do {$ConfirmInit = Read-Host "Press C to CLEAR this Disk, or X to EXIT (and press Enter)"} until ($ConfirmInit -eq 'C' -or $ConfirmInit -eq 'X') if ($ConfirmInit -eq 'X') {Break} #Virtual Machines have issues using PowerShell for Clear-Disk #$OSDDisk | Clear-Disk -RemoveOEM -RemoveData -Confirm:$true -PassThru -ErrorAction SilentlyContinue | Out-Null Write-Warning "DISKPART select disk $($OSDDisk.Number)" Write-Warning "DISKPART clean" $null = @" select disk $($OSDDisk.Number) clean exit "@ | diskpart.exe } } #====================================================================================================== # Clear additional Dirty Disks #====================================================================================================== $global:OSDDirtyDisks = $global:OSDDirtyDisks | Where-Object {$_.Number -ne $OSDDisk.Number} foreach ($item in $global:OSDDirtyDisks) { Write-Host "Secondary Disk: Clear Disk $($item.Number) $($item.FriendlyName) ($([math]::Round($item.Size / 1000000000))GB $($item.PartitionStyle)) BusType=$($item.BusType) Partitions=$($item.NumberOfPartitions)" -ForegroundColor Yellow if ($global:OSDDiskSandbox -eq $false) { $null = $ConfirmClearDisk do {$ConfirmClearDisk = Read-Host "Press C to CLEAR this disk, or S to SKIP (and press Enter)"} until ($ConfirmClearDisk -eq 'C' -or $ConfirmClearDisk -eq 'S') #$DirtyFixedDisk | Clear-Disk -RemoveOEM -RemoveData -Confirm:$false -PassThru -ErrorAction SilentlyContinue | Out-Null $null = @" select disk $($item.Number) clean exit "@ | diskpart.exe } } <# #====================================================================================================== # Simulation #====================================================================================================== if (Get-OSDGather -Property IsUEFI) { Write-Verbose "Disk $($OSDDisk.Number) will be Initialized as GPT (UEFI)" -Verbose $PartitionStyle = 'GPT' Write-Verbose "Initialize Disk $($OSDDisk.Number) $($OSDDisk.FriendlyName) ($([math]::Round($OSDDisk.Size / 1000000000))GB) $PartitionStyle" -Verbose Write-Verbose "Disk $($OSDDisk.Number) System Partition $($SizeSystemGpt / 1MB)MB FAT32 $LabelSystem" -Verbose Write-Verbose "Disk $($OSDDisk.Number) MSR Partition $($SizeMSR / 1MB)MB" -Verbose if ($SkipRecoveryPartition.IsPresent) { $SizeWindows = $($OSDDisk.Size) - $SizeSystemGpt - $SizeMSR $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1) Write-Verbose "Disk $($OSDDisk.Number) Windows Partition $($SizeWindowsGB)GB NTFS $LabelWindows" -Verbose } else { $SizeWindows = $($OSDDisk.Size) - $SizeSystemGpt - $SizeMSR - $SizeRecovery $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1) Write-Verbose "Disk $($OSDDisk.Number) Windows Partition $($SizeWindowsGB)GB NTFS $LabelWindows" -Verbose Write-Verbose "Disk $($OSDDisk.Number) Recovery Partition $($SizeRecovery / 1MB)MB NTFS $LabelRecovery" -Verbose } } else { Write-Verbose "Disk will be Initialized as MBR (BIOS)" -Verbose $PartitionStyle = 'MBR' Write-Verbose "Initialize Disk $($OSDDisk.Number) $($OSDDisk.FriendlyName) ($([math]::Round($OSDDisk.Size / 1000000000))GB) $PartitionStyle" -Verbose Write-Verbose "Disk $($OSDDisk.Number) System Partition $($SizeSystemMbr / 1MB)MB FAT32 $LabelSystem" -Verbose if ($SkipRecoveryPartition.IsPresent) { $SizeWindows = $($OSDDisk.Size) - $SizeSystemMbr - $SizeMSR $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1) Write-Verbose "Disk $($OSDDisk.Number) Windows Partition $($SizeWindowsGB)GB NTFS $LabelWindows" -Verbose } else { $SizeWindows = $($OSDDisk.Size) - $SizeSystemMbr - $SizeRecovery $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1) Write-Verbose "Disk $($OSDDisk.Number) Windows Partition $($SizeWindowsGB)GB NTFS $LabelWindows" -Verbose Write-Verbose "Disk $($OSDDisk.Number) Recovery Partition $($SizeRecovery / 1MB)MB NTFS $LabelRecovery" -Verbose } } #> #====================================================================================================== # Get OSDDisks # Update the OSDDisk information #====================================================================================================== $OSDDisk = Get-Disk -Number $OSDDisk.Number #====================================================================================================== # RAW # Force: Make sure that the OSDDisk is RAW, if not we need to exit # Simulation: No need to evaluate #====================================================================================================== if (($global:OSDDiskSandbox -eq $false) -and ($OSDDisk.PartitionStyle -ne 'RAW')) {Write-Warning "OSDDisk does not have RAW PartitionStyle. $Title cannot create additional Partitions. Exiting"; Break} #====================================================================================================== # Display OSDDisk # Show the current information about this Disk. At this point, it should be RAW #====================================================================================================== Write-Host "=================================================================================================" -ForegroundColor Cyan Write-Host "Disk $($OSDDisk.Number) - $($OSDDisk.FriendlyName) ($([math]::Round($OSDDisk.Size / 1000000000))GB $($OSDDisk.PartitionStyle)) BusType=$($OSDDisk.BusType)" -ForegroundColor Cyan Write-Host "=================================================================================================" -ForegroundColor Cyan #====================================================================================================== # AskPartition # Ask if it is ok to Partition the Drive # Simulation: No need to evaluate # P to Partition # X to exit #====================================================================================================== if ($global:OSDDiskSandbox -eq $false) { do {$AskPartition = Read-Host "Press P to Partition this Disk, or X to quit (and press Enter)"} until ($AskPartition -eq 'P' -or $AskPartition -eq 'X') if ($AskPartition -eq 'X') {Break} } #====================================================================================================== # Initialize-OSDDisk # OSDDisk is RAW so it needs to be Initialized #====================================================================================================== Initialize-OSDDisk -DiskNumber $($OSDDisk.Number) #====================================================================================================== # New-OSDPartitionSystem #====================================================================================================== New-OSDPartitionSystem -DiskNumber $($OSDDisk.Number) -SizeSystemMbr $SizeSystemMbr -SizeSystemGpt $SizeSystemGpt -LabelSystem $LabelSystem -SizeMSR $SizeMSR if ($SkipRecoveryPartition.IsPresent) { New-OSDPartitionWindows -DiskNumber $($OSDDisk.Number) -LabelWindows $LabelWindows -SkipRecoveryPartition } else { New-OSDPartitionWindows -DiskNumber $($OSDDisk.Number) -LabelWindows $LabelWindows -LabelRecovery $LabelRecovery -SizeRecovery $SizeRecovery } #====================================================================================================== # Parameters #====================================================================================================== if (-not ($Force.IsPresent)) { Write-Host "=================================================================================================" -ForegroundColor Cyan Write-Host "Parameters" -ForegroundColor Cyan Write-Host "=================================================================================================" -ForegroundColor Cyan Write-Host "-Title: $Title" -ForegroundColor Cyan Write-Host "-DiskNumber: $DiskNumber" -ForegroundColor Cyan Write-Host "-LabelSystem: $LabelSystem" -ForegroundColor Cyan Write-Host "-LabelWindows: $LabelWindows" -ForegroundColor Cyan if (Get-OSDGather -Property IsUEFI) { Write-Host "-LabelRecovery (GPT): $LabelRecovery" -ForegroundColor Cyan Write-Host "-SizeSystemGpt (GPT): $($SizeSystemGpt/ 1MB)MB" -ForegroundColor Cyan Write-Host "-SizeMSR (GPT): $($SizeMSR / 1MB)MB" -ForegroundColor Cyan Write-Host "-SizeRecovery (GPT): $($SizeRecovery/ 1MB)MB" -ForegroundColor Cyan Write-Host "-SkipRecoveryPartition (GPT): $SkipRecoveryPartition" -ForegroundColor Cyan } else { Write-Host "-SizeSystemMbr (MBR): $($SizeSystemMbr/ 1MB)MB" -ForegroundColor Cyan } Write-Host "-Force: $Force" -ForegroundColor Cyan } } |