Error running Win10 VirtualDesktop Optimization.ps1 during packer build

I have a packer build working pretty well now to create an base image to deploy into WVD. As one of the last steps in the build, I am trying to run the MS optimisation script to optimise the VM before closing up. To run the script which has a bunch of config files in subdirs, I run wrapper script which copies the files to the VM from an Azure file share and then uses invoke-expression to run the script from the local machine.

I can get the script to run however I have two problems -

  1. If I try running the script using elevated_user (as recommended by the authors), the script doesn’t run at all and packer just hangs. I am using a Win 10 Ent 20H2 sku
  2. If I just let it run (without elevation), it appears to do things but seemingly randomly during the process, it freezes for about 5 minutes, then displays an error -

==> azure-arm: . : The term ‘c:/Windows/Temp/packer-ps-env-vars-602bac0f-3b9b-dd95-3b92-f7f71739eaa8.ps1’ is not recognized as the
==> azure-arm: name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
==> azure-arm: included, verify that the path is correct and try again.
==> azure-arm: At line:1 char:138
==> azure-arm: + … ontinue’};. c:/Windows/Temp/packer-ps-env-vars-602bac0f-3b9b-dd95-3b9 …
==> azure-arm: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> azure-arm: + CategoryInfo : ObjectNotFound: (c:/Windows/Temp…7f71739eaa8.ps1:String) , CommandNotFoundException
==> azure-arm: + FullyQualifiedErrorId : CommandNotFoundException
==> azure-arm:

I get the same error just with a different GUID on the end. Normally if I run the script as default, it falls over during the services section. If I run the optimisation script with different parameters to avoid services, then it falls over in the scheduled task section (which ran fine in the previous run).

As a note, I use the same wrapper script to run all my application installs which work fine.

Any thoughts?