Citrix App Layering Preparation Script

This script can be used to prepare layers right before they are finalised.

To install and configure App Layering 4.x see: https://jgspiers.com/installing-configuring-unidesk-4/

To create and update an OS Layer see: https://jgspiers.com/create-update-os-layer-unidesk-4/

To create and update an Application Layer and create Elastic Layers see: https://jgspiers.com/application-layers-elastic-layering-unidesk-4/

Script author: George Spiers – @JGSpiers

With thanks to Trentent Tye for allowing me to include his Ghost Device removal script.

Tested working on OS version: Windows Server 2016, Windows Server 2012 R2, Windows Server 2008 R2, Windows 10, Windows 7.

Description: This scripts runs a number of tasks to clean out temporary folders, clear out Event Log entries, remove ghost devices from Device Manager, and run NGEN native image regenerations before shutting down the image in preparation for layer finalisation. If running this script on Windows 8.1+ or Windows Server 2012 R2+ there is an extra firewall check that looks to see if any of the firewall profiles are disabled and if so, asks if you want to enable them. When patching within an OS Layer version my patching software requires a number of ports to be open and it is easier just to temporarily disable the firewall until patching is done. The problem was remembering to enable the firewall again!

Note: You should run this script each time you create an OS Layer version, Application Layer/version and Platform Layer/version.

Instructions: You must run this script as an administrator. You must also set the PowerShell Execution Policy to atleast “RemoteSigned” or “Unrestricted”.

Download (click below):

App Layering Preparation Script (9995 downloads)


53 Comments

  • ciprian

    August 21, 2017

    download link it’s not working ..yet

    Reply
  • ciprian

    August 21, 2017

    working now.

    Reply
  • Rotem Sfarad

    August 23, 2017

    sound very helpful thanks buddy ;).

    Reply
  • Nick Panaccio

    August 23, 2017

    Out of curiosity, is there any reason you’re asking for the full path of the script on line 52 instead of using something like the following?

    $ScriptPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

    Either way, thank you for this.

    Reply
    • George Spiers

      August 23, 2017

      No reason at all! Your suggestion is one less input so I’ll stick it in the script. Thanks

      Reply
  • Pingback: App Layering – App Layers, Image Templates, and Publishing – Carl Stalhood

  • Pingback: Detailed Change Log – Carl Stalhood

  • Ci

    September 14, 2017

    i ran the script, but sometimes i got error on ngen still ran in background…i have to choose shutdown fro finalized exe provide by unidesk. But i did notice that if i just unidesk exe , my PVS will become non genius

    Reply
  • Pingback: App Layering – OS Layer and Platform Layer – Carl Stalhood

  • R

    October 16, 2017

    Sorry, does anybody else have a problem with the zip not opening ? Says Zip file is invalid

    Reply
    • George Spiers

      October 16, 2017

      What program are you using to unzip?

      Reply
      • R

        October 18, 2017

        windows 7 also tried 7zip …. both said invalid file …..

        Reply
        • George Spiers

          October 18, 2017

          No issue here. Windows 10 using 7zip.

          Reply
          • R

            October 19, 2017

            my ver of 7zip was too old , updated 7zip and is working now … Thanks for checking

          • George Spiers

            October 19, 2017

            Thanks for letting me know the issue!

  • R

    October 19, 2017

    Have you had any issues with the script removing ALL network adapters?
    That happened to me when i ran it finalizing/optimizing a platform layer (xenserver,pvs, xendesktop , vda 7.9 )

    Reply
    • Nick Panaccio

      October 19, 2017

      I haven’t noticed it removing all adapters, but I did find that it was removing some ghost devices that I actually needed. I wound up creating a “Lite” version of the script that displayed the ghost devices, but did not actually remove them.

      Reply
    • George Spiers

      October 19, 2017

      It should not remove any device other than “Ghost Devices” as Nick mentions. You could disable that part of the script to prevent such behaviour.

      Reply
      • RP

        October 25, 2017

        After looking into this again , the network adapter is showing fine when i finalize the platform layer , i can build an image ,publish , networking is fine .

        When I add a version to the platform layer , that’s when I lose the network adapter …., network connection folder is emty , device manager still shows the adapter as working ?

        Reply
  • Vidit Desai

    April 18, 2018

    Is there a way to set Execution Policy back to “Restricted” prior to the “Shutdown for Finalize” command executes in the script?

    Reply
    • George Spiers

      April 19, 2018

      Should be able to set “Set-ExecutionPolicy Restricted” before line 476.
      You can also run “PowerShell.exe -ExecutionPolicy Unrestricted C:\AppLayeringImagePrep.ps1” from an elevated CMD or else the run box, which temporarily changed the execution mode to unrestricted just to execute the script.

      Reply
  • Vijay

    May 21, 2018

    I ran this script at the plaform layer after i installed the VDA. I noticed that at the end of the script, it automatically shutdown the machine. Do i need to reboot the machine and click Finalize and shutdown at the platform layer again or did the PS script already did it for me. I was n’t sure.

    Can you please let em know

    Thanks,
    Vijay

    Reply
    • Nick Panaccio

      May 21, 2018

      No, you don’t have to boot the device back up and Finalize. The last line in the PS script actually calls “C:\’Program Files’\Unidesk\Uniservice\ShutdownForFinalize.cmd”, which is exactly what the desktop shortcut is pointed to. You just need to Finalize the layer from within the ELM console after the VM has completely shut down.

      Reply
      • George Spiers

        May 21, 2018

        Nick is correct – thanks Nick.

        Reply
  • andrew

    May 24, 2018

    the app layering script is awesome! only change i made was changed the ngen.exe update to ngen.exe executequeditems (we were given that method from a Unidesk tech years ago so goes much quicker…not sure if we’re missing anything going that route)
    Also, is it normal for this ps1 script to delete itself after it runs/shuts down? it seems to run successfully and shuts the packaging machine down but when i go back in to use it again if need be, its gone and i have to recopy it again

    Reply
    • George Spiers

      May 25, 2018

      Thanks – Yes I set it to do that as I don’t like the script to be left on every layer I create.
      You can edit the script and remove the self-deletion piece if you want.

      Reply
  • Jonathan Phillippe

    July 19, 2018

    I love this script and have been using it for maybe 6 months. After upgrading to Windows 10 1703.1 the portion of the script at the end that deletes files stopped running. Any ideas?

    Reply
    • George Spiers

      July 21, 2018

      If you download the script again the errors will be fixed. Was an issue with deleting files from Recycle Bin on newer W10 OS versions.

      Reply
  • Shannon C Meyers

    December 7, 2018

    Love the Script.

    Reply
  • rankam77

    January 23, 2019

    Do you have similar script for Windows 10 1803?

    Reply
    • George Spiers

      January 28, 2019

      I have used the script against W10 1803 and it works fine.

      Reply
      • Anonymous

        February 1, 2019

        Failed to load dependency IpamServer of assembly Microsoft.Windows.ServerManager.Ipam.Plugin, Version=10.0.0.0, Culture=
        neutral, PublicKeyToken=31bf3856ad364e35 because of the following error : The system cannot find the file specified. (Ex
        ception from HRESULT: 0x80070002)

        Reply
      • CJeffers

        March 25, 2019

        I’m using it against W10 1809 LTSC and it’s been working fine.

        Reply
  • rankam77

    February 1, 2019

    Can we ignore below error?

    Failed to load dependency IpamServer of assembly Microsoft.Windows.ServerManager.Ipam.Plugin, Version=10.0.0.0, Culture=
    neutral, PublicKeyToken=31bf3856ad364e35 because of the following error : The system cannot find the file specified. (Ex
    ception from HRESULT: 0x80070002)

    Reply
    • George Spiers

      February 2, 2019

      Can you ignore it? Yes. That message isn’t caused by the script, but is caused by NGEN which the script is running.

      Reply
  • Anwar

    May 29, 2019

    i have around 5 Application Layers ( such as chrome, Firefox, office 365) etc.. do i need run the script each time i create the OS Layer version as well as on Platform Layer and application layers as well? OR only on OS Layer version??

    Reply
    • George Spiers

      May 29, 2019

      I suggest running it on every layer.

      Reply
  • Anthony Denny

    February 3, 2020

    Would this script cause MCSIO to not work correctly on 1912? I can’t get the mcsdif.vhdx to be created on the MCSWCDisk.

    Reply
    • George Spiers

      May 10, 2020

      No there is nothing I am aware that would cause this.

      Reply
    • Jonathan Pitre

      September 24, 2020

      @Anthony Denny Were you using a scripted install of the VDA client ? If so I had the same problem. The fix was to change the command line slighly to this instead

      ‘/noreboot /quiet /enable_remote_assistance /disableexperiencemetrics /virtualmachine /INSTALL_MCSIO_DRIVER /noresume /enable_real_time_transport /enable_hdx_ports /enable_hdx_udp_ports /exclude “User Personalization layer”,”Citrix Files for Outlook”,”Citrix Files for Windows”,”Citrix Supportability Tools”,”Citrix Telemetry Service”,”Citrix Personalization for App-V – VDA” /components vda /mastermcsimage’

      Reply
  • Jonathan Phillippe

    February 4, 2020

    I’ve been using your script for over a year with great results. Thanks for your help.

    There was some big changes in the Citrix ELM recently and I’m using Version 20.1.0.15 and running the script gives me some different messages than in the past and I wanted to see if it is still good to go. Started getting messages like:

    “a Microsoft NGen operation is in progress in the background {0},
    a Microsoft NGen operation is needed”

    Reply
    • Jonathan Pitre

      September 24, 2020

      Citrix App Layering now recommends using “ngen eqi 3” instead of “ngen update”. Please update the script accordingly. Thanks

      https://docs.citrix.com/en-us/citrix-app-layering/4/layer/update-layer.html

      Reply
      • Roger

        March 24, 2021

        I’ve been using the script for 2 years but I just updated to the 2102 Release of App Layering and my machines would not shutdown until I change that one line as Jonathan mentioned.

        Reply
      • Robert Elsey

        October 29, 2021

        Just curious, why do we use ngen eqi 3 and not ngen eqi 1? As according to MS, if you run a priority 3 – Native images are installed when the native image service detects that the computer is idle. If you run priority 1 – Native images are generated and installed immediately, without waiting for idle time. Wouldn’t running a priority 1 be faster so that your not waiting for the system to be idle? Thanks.

        Reply
  • Mike

    November 23, 2020

    Thanks for the great Citrix content. Was curious if you were considering updating this script?

    Reply
  • Anonymous

    March 4, 2021

    This is a great script. I have been using it for some time. I am wondering if you might want to add a command to delete the RDS timebomb since I have seen that show up as a reason for issues quite a bit.

    Reply
  • Pingback: Citrix App Layering:  Part One, the OS Layer – Brandon Mitchell

  • mw

    November 19, 2021

    Has no one tested on Win Server 2019 still ? codename Redstone

    Reply
  • IZFE

    January 4, 2022

    Why does the space in disk goes bigger? Every time I execute the script it gets bigger.

    Reply
  • Kevin Phillips

    January 12, 2022

    Get an error when creating the Platform Layer. When it asks you to start the packaging Machine, it wont boot, it just boots the UEFI shell. I am using Server 2022 and Hypervisor 8.2. Has anybody else seen this.k

    Reply
  • Travis

    March 2, 2022

    Hey George – any ideas when this will be ready for Windows 11 ENT?

    Reply
  • Jacob Mani

    August 5, 2024

    can we use this same script for Platform layer and OS layer sealing also?

    Reply
    • George Spiers

      August 5, 2024

      Yes absolutely.

      Reply

Leave a Reply