Skip to content

Instantly share code, notes, and snippets.

@brianfgonzalez
Last active May 8, 2019 20:43
Show Gist options
  • Save brianfgonzalez/ffb52fe753b19d312986166118b28fb4 to your computer and use it in GitHub Desktop.
Save brianfgonzalez/ffb52fe753b19d312986166118b28fb4 to your computer and use it in GitHub Desktop.
This file should reside on the <MdtDeploymentShare>\Scripts folder.
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
[switch]$AddRecoveryBcd,
[Parameter(Mandatory=$false)]
[string]$TaskSequence,
[Parameter(Mandatory=$false)]
[switch]$HideRecovery
)
. ..\scripts\AppDeployToolkit\AppDeployToolkitMain.ps1
<#
If ($AddRecovery)
{
Execute-Process -Path "bcdedit" -Parameters '/copy {default} /d "Windows Recovery"' -PassThru |
% {
If ($_.StdOut -match "(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})" -eq $true)
{
$RecoveryGuid = $Matches[0]
Execute-Process -Path "bcdedit" -Parameters ('/set {0} systemroot \windows' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} detecthal yes' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} winpe yes' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} path \windows\system32\winload.efi' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} osdevice ramdisk=[\Device\Harddisk0\Volume1]\Deploy\Boot\LitetouchPE_x64.wim,{ramdiskoptions}' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} device ramdisk=[\Device\Harddisk0\Volume1]\Deploy\Boot\LitetouchPE_x64.wim,{ramdiskoptions}' -f $RecoveryGuid) -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters ('/set {0} nx optout' -f $RecoveryGuid) -ContinueOnError $true
}
}
}
#>
If ($AddRecoveryBcd)
{
"select disk 0" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii
"select partition 3" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
"assign letter=r" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
Start-Process -FilePath "diskpart.exe" -ArgumentList ('/s "{0}\diskpart.txt"' -f $env:temp) -Wait -WindowStyle Minimized
Start-Sleep -Seconds 3
Execute-Process -Path "bcdedit" -Parameters '/delete {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2}' -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters '/create {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} /d "Recovery Partition" /application osloader'
Execute-Process -Path "bcdedit" -Parameters '/create {ramdiskoptions} /d "Recovery Partition"' -ContinueOnError $true
Execute-Process -Path "bcdedit" -Parameters '/set {ramdiskoptions} ramdisksdidevice partition=r:'
Execute-Process -Path "bcdedit" -Parameters '/set {ramdiskoptions} ramdisksdipath \boot\boot.sdi'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} device ramdisk=[r:]\Deploy\Boot\LitetouchPE_x64.wim,{ramdiskoptions}'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} path \windows\system32\winload.efi'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} osdevice ramdisk=[r:]\Deploy\Boot\LitetouchPE_x64.wim,{ramdiskoptions}'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} systemroot \Windows'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} winpe yes'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} detecthal yes'
Execute-Process -Path "bcdedit" -Parameters '/set {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} nx optout'
Execute-Process -Path "bcdedit" -Parameters '/displayorder {63c34f23-5c9f-11e9-bf48-fcf8c349bbb2} /addlast'
Execute-Process -Path "bcdedit" -Parameters '/set {fwbootmgr} displayorder {bootmgr} /addfirst'
Execute-Process -Path "bcdedit" -Parameters '/enum all'
}
If ($HideRecovery)
{
"select disk 0" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii
"select partition 3" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
"remove" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
'set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac' | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
"gpt attributes=0x8000000000000000" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
Start-Process -FilePath "diskpart.exe" -ArgumentList ('/s "{0}\diskpart.txt"' -f $env:temp) -Wait -WindowStyle Minimized
Start-Sleep -Seconds 3
}
If ($TaskSequence)
{
Write-Log -Message "Attempting to hide all Task Sequence EXCEPT $TaskSequence" -Source '_RecoveryTool' -LogType 'CMTrace'
if (!(test-path 'r:\Deploy\Control\TaskSequences.xml'))
{
"select disk 0" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii
"select partition 3" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
"assign letter=r" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
Start-Process -FilePath "diskpart.exe" -ArgumentList ('/s "{0}\diskpart.txt"' -f $env:temp) -Wait -WindowStyle Minimized
Start-Sleep -Seconds 3
}
[xml]$xml = get-content 'r:\Deploy\Control\TaskSequences.xml'
$xml.tss.ts | ? { $_.name -ne "Deploy an image" } | % { $_.hide = "True" }
$xml.save('r:\Deploy\Control\TaskSequences.xml')
Write-Log -Message "Rehiding Recovery Partition" -Source '_RecoveryTool' -LogType 'CMTrace'
"select disk 0" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii
"select partition 3" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
"remove" | out-file ('{0}\diskpart.txt' -f $env:temp) -Encoding ascii -Append
Start-Process -FilePath "diskpart.exe" -ArgumentList ('/s "{0}\diskpart.txt"' -f $env:temp) -Wait -WindowStyle Minimized
Start-Sleep -Seconds 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment