Private/New-OSDPartitionSystem.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
<#
.SYNOPSIS
New-OSDDisk Private Function
 
.DESCRIPTION
New-OSDDisk Private Function
 
.NOTES
19.10.10 Created by David Segura @SeguraOSD
#>

function New-OSDPartitionSystem {
    [CmdletBinding()]
    param (
        #Fixed Disk Number
        #For multiple Fixed Disks, use the SelectDisk parameter
        #Default = 0
        #Alias = Disk, Number
        [Alias('Disk','Number')]
        [int]$DiskNumber = 0,

        #Drive Label of the System Partition
        #Default = System
        #Alias = LS, LabelS
        [Alias('LS','LabelS')]
        [string]$LabelSystem = 'System',

        #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
    )
    #======================================================================================================
    #    UEFI GPT SYSTEM + MSR
    #======================================================================================================
    if (Get-OSDGather -Property IsUEFI) {
        if ($global:OSDDiskSandbox -eq $true) {
            Write-Host "SANDBOX: New-Partition -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -DiskNumber $DiskNumber -Size $($SizeSystemGpt / 1MB)MB" -ForegroundColor DarkGray

            #Write-Host "SANDBOX: Format-Volume -FileSystem FAT32 -NewFileSystemLabel $LabelSystem" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART select disk $DiskNumber" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART select partition $($PartitionSystem.PartitionNumber)" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART format fs=fat32 quick label='$LabelSystem'" -ForegroundColor DarkGray

            Write-Host "SANDBOX: Set-Partition -GptType {c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" -ForegroundColor DarkGray
            Write-Host "SANDBOX: New-Partition -GptType {e3c9e316-0b5c-4db8-817d-f92df00215ae} Size $($SizeMSR / 1MB)MB" -ForegroundColor DarkGray
        }
        if ($global:OSDDiskSandbox -eq $false) {
            Write-Warning "New-Partition -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -DiskNumber $DiskNumber -Size $($SizeSystemGpt / 1MB)MB"
            $PartitionSystem = New-Partition -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -DiskNumber $DiskNumber -Size $SizeSystemGpt

            #Write-Warning "Format-Volume -FileSystem FAT32 -NewFileSystemLabel $LabelSystem"
            #Format-Volume -ObjectId $PartitionSystem.ObjectId -FileSystem FAT32 -NewFileSystemLabel "$LabelSystem" -Force -Confirm:$false
            Write-Warning "DISKPART select disk $DiskNumber"
            Write-Warning "DISKPART select partition $($PartitionSystem.PartitionNumber)"
            Write-Warning "DISKPART format fs=fat32 quick label='$LabelSystem'"
$null = @"
select disk $DiskNumber
select partition $($PartitionSystem.PartitionNumber)
format fs=fat32 quick label="$LabelSystem"
exit
"@
 | diskpart.exe

            Write-Warning "Set-Partition -GptType {c12a7328-f81f-11d2-ba4b-00a0c93ec93b}"
            $PartitionSystem | Set-Partition -GptType '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}'
            Write-Warning "New-Partition GptType {e3c9e316-0b5c-4db8-817d-f92df00215ae} Size $($SizeMSR / 1MB)MB"
            $null = New-Partition -DiskNumber $DiskNumber -Size $SizeMSR -GptType '{e3c9e316-0b5c-4db8-817d-f92df00215ae}'
        }
    }
    #======================================================================================================
    #    BIOS MBR SYSTEM
    #======================================================================================================
    if (! (Get-OSDGather -Property IsUEFI)) {

        if ($global:OSDDiskSandbox -eq $true) {
            Write-Host "SANDBOX: New-Partition Size $($SizeSystemMbr / 1MB)MB IsActive" -ForegroundColor DarkGray
            
            #Write-Host "SANDBOX: Format-Volume FileSystem NTFS NewFileSystemLabel $LabelSystem" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART select disk $DiskNumber" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART select partition $($PartitionSystem.PartitionNumber)" -ForegroundColor DarkGray
            Write-Host "SANDBOX: DISKPART format fs=ntfs quick label='$LabelSystem'" -ForegroundColor DarkGray
        }
        
        if ($global:OSDDiskSandbox -eq $false) {

            Write-Warning "New-Partition Size $($SizeSystemMbr / 1MB)MB IsActive"
            $PartitionSystem = New-Partition -DiskNumber $DiskNumber -Size $SizeSystemMbr -IsActive

            #Write-Warning "Format-Volume FileSystem NTFS NewFileSystemLabel $LabelSystem"
            #Format-Volume -ObjectId $PartitionSystem.ObjectId -FileSystem NTFS -NewFileSystemLabel "$LabelSystem" -Force -Confirm:$false
            Write-Warning "DISKPART select disk $DiskNumber"
            Write-Warning "DISKPART select partition $($PartitionSystem.PartitionNumber)"
            Write-Warning "DISKPART format fs=ntfs quick label='$LabelSystem'"
$null = @"
select disk $DiskNumber
select partition $($PartitionSystem.PartitionNumber)
format fs=ntfs quick label="$LabelSystem"
exit
"@
 | diskpart.exe

        }
    }
}