Jul 292019
 

Update: Added cli functionality.
Update: Added 2nd pass of defrag to fix the profile ballooning issue that sometimes occurs.
Update: Added ability to target profiles over “X” size for compacting
Update 3/31/2020: Fixed a bug that didn’t allow the new tool to run as a scheduled task. Fixed in version 2003.1
Update 4/21/2020: Fixed bug that didn’t assign drive letters to vhd(x) files when running via CLI. Which breaks the defrag, and in turn doesn’t free up any space for diskpart to shrink. Fixed version is 2004.1.
Update 4/23/2020: Added the ability to sort profiles in the GUI. Clicking the stop button should dismount vhd(x) files automatically. Uses optimize-volume instead of defrag – should be MUCH faster. Also does not need drive letters anymore. Version 2004.2
Update 4/27/2020: Tool now detects RW disks and will not attempt to compact multi session profile disks if in use. Version 2004.4
Update 4/29/2020: Tool will find hidden .vhd(x) files. Version 2004.5
Update 5/26/2020: Fixed the optimize-volumes to use “retrim” after defraging the volume. This will increase processing time, but shrink the vhd(x) files much more. Version 2005.1

Update 6/24/2020: Added defrag back – which seems to do a lot better at freeing up space. Instead of giving a drive letter it sets the drive up as a mount point in the TEMP directory of the user running it (random named folder that will start with “_FSL”). With this I have updated the command line so you can process more than one vhd(x) at a time. TAKE NOTE OF THE NEW COMMAND LINE OPTIONS AS THEY HAVE CHANGED! Version 2006.1
UPDATE 6/25/2020: Bug fix – the 2006.1 version would leave the last vhd(x) attached when using the -tasks # switch. No, popups in cli to stop processing if it doesn’t find any vhd(x), and instead of “N/A” it will show the file size if the file is locked in the after column. Fixed in version 2006.3.
Edit: Version 2006.4 now – last bug fix for the day hopefully. Forgot to add the check for RW.VHD(X) files.

Do not use this tool if you are doing any differencing disks!
Diff disks, no problem!

This post is an updated version of my original profile compacting script ( http://www.citrixirc.com/?p=829). I wrote it using Powershell Studio and converted it into an executable. This version does not require the Hyper-V powershell module as it diskpart to perform the shrink function.

Requirements:

  1. Administrative rights on the machine running the tool, and read/write to the profiles.
  2. .Net 4.5
  3. Recommend not running from a machine which has any .vhd(x) attached

Instructions/How it works:

  1. The program will remember the last directory selected (via an .ini file – if it exists), or you can click the “…” button at the top left to browse to the root directory of your profile share.
  2. Once a directory is selected it will list all .vhd and .vhdx files along with their current size and their current locked status in descending order by size. (this can take a little while depending on how many vhd(x) files are present – be patient)
  3. Select the profile(s) you wish to compact (you can select multiple using ctrl and shift+click)
  4. Click the compact button, and the program will process each profile selected one at a time
    1. Checks one more time to make sure the file is not locked
      1. If it is, it will skip on to the next one
    2. Gets the current size again before processing
    3. Attaches the vhd(x) in R/W mode
    4. Creates a random named folder in the user’s temp directory starting with “_FSL”
    5. Mounts the vhd(x) to that folder
    6. defrags the volume
    7. Detaches and re-attaches as read only
    8. Uses diskpart to compact the vhd(x)
    9. Gets the size of the file post processing
    10. Updates the results pane
  5. After it has run through all the selected profiles it will display the total reduction in MB at the bottom.
  6. Update: To run via CLI simply run from command prompt with the following options
    -path \\servername\share (path to the root vhd(x) share)
    -size 4096 (minimum size to touch in MB – will skip any vhd(x) smaller)
    -tasks # (number of concurrent vhd(x) files to compact – there is no limit, so be careful not to overload the machine you’re running this on)

    Examples:
    Run against all vhd(x) files one at a time
    ShrinkFSL.exe -path \\servername\share\

    Run against all vhd(x) files 2 at a time
    ShrinkFSL.exe -path \\servername\share -tasks 2

    Run against all vhd(x) files over 5GB
    ShrinkFSL.exe -path \\servername\share -size 5120

    Run against all vhd(x) files over 5GB AND process 2 at a time
    ShrinkFSL.exe -path \\servername\share -size 5120 -tasks 2
    1. A log file in csv format will be generated in the same directory that ShrinkFSL is run from. Shrink_MMddyyy_HHmmss.log
    2. If you stop the process make sure you disconnect any vhd(x) file that may be lingering, and delete _FSL folders in the temp directory!

You can keep track of the .vhd(x) attaching/detaching via diskmgmt.msc if you want. If for some reason the program hangs up you can click the stop button. It will detect if there is a vhd(x) attached, and detach it.

Always test new tools in Development/UAT environments prior to running in production! If you have any questions/comments please post here, and I will respond as soon as I can.

Here is a link to the tool (Updated 6/25/2020)
https://david62277.sharefile.com/d-saad8432c8994f61b