Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

How to relocate the Package Cache

View Move-WixPackageCache.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
<#
This license governs use of the accompanying software. If you use the software, you
accept this license. If you do not accept the license, do not use the software.
1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
same meaning here as under U.S. copyright law.
A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#>
 
#Requires -Version 3.0
 
[CmdletBinding()]
param
(
[Parameter(Position=0, Mandatory=$true)]
[string] $Path,
 
[Parameter()]
[ValidateRange(0, 2TB)]
[long] $Size = 1TB
)
 
# Require elevated privileges (Tip: PSv4 supports '#Requires -RunAsAdministrator').
$Principal = New-Object System.Security.Principal.WindowsPrincipal ([System.Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{
Write-Error 'Elevated privileges are required to move the package cache.' -Category PermissionDenied
return
}
 
# Make sure modules are loaded (Tip: PSv3 supports '#Requires -Modules <String[]>').
Import-Module 'Dism', 'Storage', 'ScheduledTasks'
 
$VhdPath = Join-Path $Path 'Cache.vhd'
$CachePath = Join-Path $env:ProgramData 'Package Cache'
 
# Make sure the necessary Windows features are enabled.
$Features = 'Microsoft-Hyper-V-All', 'Microsoft-Hyper-V-Tools-All', 'Microsoft-Hyper-V-Management-PowerShell' | ForEach-Object {
Get-WindowsOptionalFeature -Online -FeatureName $_ -Verbose:$false
} | Where-Object { $_.State -ne 'Enabled' } | Select-Object -expand FeatureName
if ($Features)
{
Write-Verbose "Installing required Windows feature(s): $($Features -split ', ')"
Enable-WindowsOptionalFeature -Online -FeatureName $Features -NoRestart -Verbose:$false
if (-not $?)
{
return
}
}
 
# Create, partition, and format the cache VHD.
if (-not (Test-Path $Path -PathType Container))
{
Write-Verbose "Creating directory for virtual disk: $Path"
$null = New-Item $Path -Type 'Directory'
}
elseif (Test-Path "$VhdPath" -PathType Leaf)
{
Write-Error "A virtual disk already exists at `"$VhdPath`"." -Category ResourceExists
return
}
 
Write-Verbose "Creating virtual disk: $VhdPath"
$Disk = New-VHD "$VhdPath" -SizeBytes $Size -Dynamic | Mount-VHD -NoDriveLetter -Passthru
if (-not $?)
{
return
}
 
$Acl = New-Object System.Security.AccessControl.FileSecurity
$Acl.SetSecurityDescriptorSddlForm('O:BAG:DUD:P(A;;FA;;;BA)(A;;FA;;;SY)(A;;FRFX;;;BU)(A;;FRFX;;;WD)', 'All')
$Acl | Set-Acl $Disk.Path
 
$Partition = Initialize-Disk $Disk.Number -PassThru | New-Partition -UseMaximumSize
$null = $Partition | Format-Volume -FileSystem NTFS -NewFileSystemLabel 'Package Cache' -Confirm:$false
$Volume = $Partition | Add-PartitionAccessPath -AssignDriveLetter -PassThru | Get-Volume
 
$Drive = "$($Volume.DriveLetter):"
$Acl = New-Object System.Security.AccessControl.DirectorySecurity
$Acl.SetSecurityDescriptorSddlForm('O:BAG:DUD:PAI(A;OICIID;FA;;;BA)(A;OICIID;FA;;;SY)(A;OICIID;FRFX;;;BU)(A;OICIID;FRFX;;;WD)', 'All')
Set-Acl "$Drive\" -AclObject $Acl
Write-Verbose "Created and formatted drive: $Drive, label: Package Cache"
 
# Copy the existing package cache (if any).
if (Test-Path "$CachePath" -PathType Container)
{
Write-Verbose "Copying contents from: $CachePath; to: $Drive"
Copy-Item "$CachePath\*" "$Drive\" -Recurse
if (-not $?)
{
return
}
 
Write-Verbose "Deleting directory: $CachePath"
Remove-Item "$CachePath" -Force -Recurse
if (-not $?)
{
return
}
}
 
# Create the empty directory and mount the VHD into it.
Write-Verbose "Creating empty directory: $CachePath"
New-Item "$CachePath" -Type 'Directory' -Force | Set-Acl -AclObject $Acl
if (-not $?)
{
return
}
 
Write-Verbose "Mounting virtual disk into directory: $CachePath"
$Partition | Add-PartitionAccessPath -AccessPath "$CachePath" -PassThru | Remove-PartitionAccessPath -AccessPath "$Drive"
Write-Verbose "Removed drive letter: $Drive"
 
# Schedule a task to make sure the VHD is always mounted on boot.
Write-Verbose "Creating script to mount VHD: $VhdPath, path: $ScriptPath"
$ScriptPath = [System.IO.Path]::ChangeExtension($VhdPath, '.txt')
@"
select vdisk file=$VhdPath
attach vdisk
"@ | Set-Content $ScriptPath -Force
Get-Acl $VhdPath | Set-Acl $ScriptPath
 
Write-Verbose "Scheduling task to mount VHD: $VhdPath"
$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute (get-command diskpart.exe).Path -Argument "/s `"$ScriptPath`""
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$user = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -RunLevel Highest -LogonType ServiceAccount
$null = Register-ScheduledTask -TaskName 'Attach Package Cache' -Trigger $trigger -Action $action -Settings $settings -Principal $user -Force
 
<#
.Synopsis
Move the Burn package cache to another drive or directory.
.Description
Actually moving the Burn package cache to another drive or directory is currently not supported.
Instead this script creates a expandable virtual disk (VHD) in whatever directory you want - even on another drive.
Any existing payload is copied and the expandable virtual disk is mounted into the now empty package cache.
.Parameter Path
The directory in which the package cache is moved.
.Parameter Size
The maximum size of the expandable virtual disk to create. The default is 1TB.
.Notes
You need administrator privileges to run this script.
.Link
http://aka.ms/mvcache
#>

Hi,
It does not work on my Surface:
DiskPart has encountered an error: The security ID structure is invalid
any idea?
Patrice

Owner

@psallandre, I'm not sure without more detail about where it happened in the script (i.e. a stack trace would be more helpful) but since you only have one drive why would you move the files to a VHD sitting on the drive? While not very much, the VHD does have overhead so you'd actually be increasing drive space consumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.