Public/OSDCloudRE.Functions.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
function Get-OSDCloudREPartition {
    <#
    .SYNOPSIS
    OSDCloudRE: Gets the OSDCloudRE Partition object
     
    .DESCRIPTION
    OSDCloudRE: Gets the OSDCloudRE Partition object
     
    .EXAMPLE
    Get-OSDCloudREPartition
     
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    
    [CmdletBinding()]
    [OutputType([Microsoft.Management.Infrastructure.CimInstance])]
    [OutputType('Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Partition')]
    param ()
    Write-Verbose $MyInvocation.MyCommand

    Get-OSDCloudREVolume | Get-Partition
}
function Get-OSDCloudREPSDrive {
    <#
    .SYNOPSIS
    OSDCloudRE: Gets the OSDCloudRE PSDrive object
 
    .DESCRIPTION
    OSDCloudRE: Gets the OSDCloudRE PSDrive object
 
    .EXAMPLE
    Get-OSDCloudREPSDrive
 
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    
    [CmdletBinding()]
    [OutputType([System.Management.Automation.PSDriveInfo])]
    param ()
    Write-Verbose $MyInvocation.MyCommand
    
    Get-PSDrive | Where-Object {$_.Description -eq 'OSDCloudRE'}
}
function Get-OSDCloudREVolume {
    <#
    .SYNOPSIS
    OSDCloudRE: Gets the OSDCloudRE Volume object
 
    .DESCRIPTION
    OSDCloudRE: Gets the OSDCloudRE Volume object
 
    .EXAMPLE
    Get-OSDCloudREVolume
 
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    
    [CmdletBinding()]
    [OutputType([Microsoft.Management.Infrastructure.CimInstance])]
    [OutputType('Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume')]
    param ()
    Write-Verbose $MyInvocation.MyCommand
    
    Get-Volume | Where-Object {$_.FileSystemLabel -match 'OSDCloudRE'}
}
function Hide-OSDCloudREDrive {
    <#
    .SYNOPSIS
    OSDCloudRE: Hides the OSDCloudRE Drive
     
    .DESCRIPTION
    OSDCloudRE: Hides the OSDCloudRE Drive
     
    .EXAMPLE
    Hide-OSDCloudREDrive
     
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>


    [CmdletBinding()]
    [OutputType([System.Void])]
    param ()
    Write-Verbose $MyInvocation.MyCommand

    Block-StandardUser
    $OSDCloudREPartition = Get-OSDCloudREPartition

    if ($OSDCloudREPartition) {
$null = @"
select disk $($OSDCloudREPartition.DiskNumber)
select partition $($OSDCloudREPartition.PartitionNumber)
remove
set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
gpt attributes=0x8000000000000001
exit
"@
 | diskpart.exe
    }
    else {
        Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find an OSDCloudRE partition"
    }
}
function New-OSDCloudREVolume {
    <#
    .SYNOPSIS
    OSDCloudRE: Gets the OSDCloudRE Partition object
     
    .DESCRIPTION
    OSDCloudRE: Gets the OSDCloudRE Partition object
     
    .EXAMPLE
    New-OSDCloudREVolume
     
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    
    [CmdletBinding()]
    [OutputType([Microsoft.Management.Infrastructure.CimInstance])]
    [OutputType('Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume')]
    param (
        [int32]$IsoSize = 1038090240
    )
    Write-Verbose $MyInvocation.MyCommand

    Block-StandardUser

    $WindowsPartition = Get-Partition | Where-Object {$env:SystemDrive -match $_.DriveLetter}
    $WindowsDiskNumber = $WindowsPartition.DiskNumber
    $WindowsSizeMax = $WindowsPartition | Get-PartitionSupportedSize | Select-Object -ExpandProperty SizeMax
    $WindowsShrinkSize = $WindowsSizeMax - $IsoSize - 200MB
    $OSDCloudREVolume = Get-OSDCloudREVolume
    #============================================
    # Test WindowsPartition
    #============================================
    if ($WindowsPartition) {
        #============================================
        # Test UEFI
        #============================================
        if ((Get-OSDGather -Property IsUEFI)) {
            #============================================
            # Test if OSDCloudRE already exists
            #============================================
            if (! $OSDCloudREVolume) {
                #============================================
                # Shrink Windows Partition
                #============================================
                Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Shrinking Windows partition"
                $WindowsPartition | Resize-Partition -Size $WindowsShrinkSize
                #============================================
                # Test WindowsPartition
                # Get Results
                #============================================
                if ($WindowsPartition) {
                    #============================================
                    # Create NewPartition
                    #============================================
                    Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Creating OSDCloudRE Partition"
                    $Global:NewPartition = New-Partition -DiskNumber $WindowsDiskNumber -GptType '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}' -UseMaximumSize
                    #============================================
                    # Test NewPartition
                    #============================================
                    if ($Global:NewPartition) {
                        #============================================
                        # Test NewPartitionNumber
                        #============================================
                        $Global:NewPartitionNumber = $Global:NewPartition.PartitionNumber
                        #============================================
                        # Format Partition
                        #============================================
                        if ($Global:NewPartitionNumber) {
                        
                            $Global:FormatVolume = Format-Volume -Partition $Global:NewPartition -FileSystem NTFS -NewFileSystemLabel 'OSDCloudRE' -Force
                            $Global:PartitionAccessPath = Add-PartitionAccessPath -AccessPath O: -DiskNumber $Global:NewPartition.DiskNumber -PartitionNumber $Global:NewPartition.PartitionNumber

                            Write-Host -ForegroundColor DarkGray "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Testing OSDCloudRE Volume"
                            #============================================
                            # Return Results
                            #============================================
                            if (Get-OSDCloudREVolume) {
                                Get-OSDCloudREVolume
                            }
                            else {
                                Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Could not create OSDCloudRE volume"
                            }
                        }
                        else {
                            Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to get OSDCloudRE partition DiskNumber"
                        }
                    }
                    else {
                        Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to create an OSDCloudRE partition"
                    }
                }
                else {
                    Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to shink Windows partition"
                }
            }
            else {
                Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Cannot create a second OSDCloudRE instance"
            }
        }
        else {
            Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) OSDCloudRE requires UEFI"
        }
    }
    else {
        Write-Error "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find the Windows Partition"
    }
}
function Set-OSDCloudREBootmgr {
    <#
    .SYNOPSIS
    OSDCloudRE: Configures OSDCloudRE Boot Manager options
 
    .DESCRIPTION
    OSDCloudRE: Configures OSDCloudRE Boot Manager options. Requires ADMIN righs
 
    .EXAMPLE
    Set-OSDCloudREBootmgr -SetRamdisk -SetOSloader
    Creates or updates the OSDCloudRE Ramdisk and OSLoader
    Requires boot content in O:\
     
    .EXAMPLE
    Set-OSDCloudREBootmgr -OSMenuAdd
    Adds OSDCloudRE to the Boot Manager Operating System selection
 
    .EXAMPLE
    Set-OSDCloudREBootmgr -OSMenuRemove
    Removes OSDCloudRE from the Boot Manager Operating System selection
 
    .EXAMPLE
    Set-OSDCloudREBootmgr -BootToOSDCloudRE
    Boots to OSDCloudRE on the next reboot
 
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    
    [CmdletBinding()]
    [OutputType([System.Void])]
    param (
        [System.Management.Automation.SwitchParameter]
        #Creates or updates the OSDCloudRE Ramdisk
        $SetRamdisk,

        [System.Management.Automation.SwitchParameter]
        #Creates or updates the OSDCloudRE OSLoader
        $SetOSloader,

        [System.Management.Automation.SwitchParameter]
        #Adds OSDCloudRE to the Boot Manager Operating System selection
        $OSMenuAdd,

        [System.Management.Automation.SwitchParameter]
        #Removes OSDCloudRE from the Boot Manager Operating System selection
        $OSMenuRemove,

        [System.Management.Automation.SwitchParameter]
        #Boots to OSDCloudRE on the next reboot
        $BootToOSDCloudRE
    )
    Write-Verbose $MyInvocation.MyCommand

    Block-StandardUser

    if ($SetRamdisk -or $SetOSloader) {
        $OSDCloudREPartition = Get-OSDCloudREPartition
        if (! $OSDCloudREPartition) {
            Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find OSDCloudRE Partition"
        }
    }

    if ($SetRamdisk) {
        if ($OSDCloudREPartition) {
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /create '{4f534452-616d-6469-736b-536567757261}' /d OSDRamdisk /device"
            $null = bcdedit /create '{4f534452-616d-6469-736b-536567757261}' /d "OSDRamdisk" /device
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534452-616d-6469-736b-536567757261}' ramdisksdidevice partition=O:"
            $null = bcdedit /set '{4f534452-616d-6469-736b-536567757261}' ramdisksdidevice partition=O:
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534452-616d-6469-736b-536567757261}' ramdisksdipath \boot\boot.sdi"
            $null = bcdedit /set '{4f534452-616d-6469-736b-536567757261}' ramdisksdipath \boot\boot.sdi
        }
    }

    if ($SetOSloader) {
        if ($OSDCloudREPartition) {
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /create '{4f534443-6c6f-7564-5245-536567757261}' /d OSDCloudRE /application osloader"
            $null = bcdedit /create '{4f534443-6c6f-7564-5245-536567757261}' /d "OSDCloudRE" /application osloader
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' device ramdisk=[O:]\sources\boot.wim,'{4f534452-616d-6469-736b-536567757261}'"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' device ramdisk=[O:]\sources\boot.wim,'{4f534452-616d-6469-736b-536567757261}'
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' osdevice ramdisk=[O:]\sources\boot.wim,'{4f534452-616d-6469-736b-536567757261}'"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' osdevice ramdisk=[O:]\sources\boot.wim,'{4f534452-616d-6469-736b-536567757261}'
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' path \windows\system32\winload.efi"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' path \windows\system32\winload.efi
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' systemroot \Windows"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' systemroot \Windows
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' detecthal Yes"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' detecthal Yes
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' winpe Yes"
            $null = bcdedit /set '{4f534443-6c6f-7564-5245-536567757261}' winpe Yes
        }
    }

    if ($OSMenuAdd) {
        Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /displayorder '{4f534443-6c6f-7564-5245-536567757261}' /addlast"
        $null = bcdedit /displayorder '{4f534443-6c6f-7564-5245-536567757261}' /addlast
    }

    if ($OSMenuRemove) {
        Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /displayorder '{4f534443-6c6f-7564-5245-536567757261}' /remove"
        $null = bcdedit /displayorder '{4f534443-6c6f-7564-5245-536567757261}' /remove
    }

    if ($BootToOSDCloudRE) {
        Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) bcdedit /bootsequence '{4f534443-6c6f-7564-5245-536567757261}'"
        try {
            $null = bcdedit /bootsequence '{4f534443-6c6f-7564-5245-536567757261}'
            Write-Verbose "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) OSDCloudRE set for next boot"
        }
        catch {
            Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) OSDCloudRE could not be set for next boot"
        }
    }
}
function Show-OSDCloudREDrive {
    <#
    .SYNOPSIS
    OSDCloudRE: Shows the OSDCloudRE Drive
     
    .DESCRIPTION
    OSDCloudRE: Shows the OSDCloudRE Drive
     
    .EXAMPLE
    Show-OSDCloudREDrive
     
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>


    [CmdletBinding()]
    [OutputType([System.Void])]
    param ()
    Write-Verbose $MyInvocation.MyCommand

    Block-StandardUser
    $OSDCloudREPartition = Get-OSDCloudREPartition

    if ($OSDCloudREPartition) {
$null = @"
select disk $($OSDCloudREPartition.DiskNumber)
select partition $($OSDCloudREPartition.PartitionNumber)
set id="ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"
gpt attributes=0x0000000000000000
assign letter=o
rescan
exit
"@
 | diskpart.exe
    }
    else {
        Write-Warning "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Unable to find an OSDCloudRE partition"
    }
}