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?

Hi,

I am having the same outcome. In my case I narrowed it down to the change of the Send Buffer Size using Set-NetAdapterAdvancedProperty.

When the Send Buffer Size is changed, it does some sort of network reset on the VM, I don’t think Packer likes this very much and it never recovers and re-establishes the WinRM connection.

I am currently trying a number of ways to get around it, but none have worked so far.

Did you manage to fix this?

Cheers
Dave

Hi davetustin,

You nailed it. The Set-NetAdapterAdvancedProperty resets the NIC. On a standard RDP connection to a host it automatically reconnects however I couldn’t get WinRM to reconnect. In the end I just commented out the line from the script -

#Set-NetAdapterAdvancedProperty -DisplayName “Send Buffer Size” -DisplayValue 4MB

Even though this is the recommendation regarding VDI in Azure, I had to go without it.

Regards,

Mat.

Hi mat,

Thanks for confirming this.

I did find that even if it does throw an error as below, the script continues to run on the guest, even though the WinRM connection has dropped and never connects again.

I checked the VDI Optimization event log and it continues to apply the local policy, clean up etc afterwards. This is the last step in my build, before a restart, so after a few minutes (my default timeout) it restart and continues to finish with a sysprep.

azure-arm.windowsvm: [VDI Optimize] Configuring Network Adapter Buffer Size
==> azure-arm.windowsvm: . : The term ‘c:/Windows/Temp/packer-ps-env-vars-60e5f2bc-d9f4-cea9-88fc-81eb677cc98e.ps1’ is not recognized as the==> azure-arm.windowsvm: name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
==> azure-arm.windowsvm: included, verify that the path is correct and try again.==> azure-arm.windowsvm: At line:1 char:138
==> azure-arm.windowsvm: + … ontinue’};. c:/Windows/Temp/packer-ps-env-vars-60e5f2bc-d9f4-cea9-88f …==> azure-arm.windowsvm: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~==> azure-arm.windowsvm: + CategoryInfo : ObjectNotFound: (c:/Windows/Temp…1eb677cc98e.ps1:String) , CommandNotFoundException==> azure-arm.windowsvm: + FullyQualifiedErrorId : CommandNotFoundException==> azure-arm.windowsvm:
==> azure-arm.windowsvm: Restarting Machine
==> azure-arm.windowsvm: Waiting for machine to restart…
==> azure-arm.windowsvm: A system shutdown is in progress.(1115) azure-arm.windowsvm: Machine restarted.

I have put a PowerShell sctript together to download the latest version, unzip it, remove the Set-NetAdapterAdvancedProperty line and then run the ps1 to apply the configuration.

I find this bombs out later on after the cleanup and the build is destroyed. Not sure why this is happening at the moment. I will try commenting out like you have done, but I wanted to always download the latest version of the script.

Here is a copy of the script if you are interested.

$location = "c:\tools"
$source   = "https://github.com/The-Virtual-Desktop-Team/Virtual-Desktop-Optimization-Tool/archive/refs/heads/main.zip"
$dest     = "$location\Virtual-Desktop-Optimization-Tool.zip"

# Allow the execution of scripts in the PowerShell session
Set-ExecutionPolicy Bypass -Scope Process -Force
# Download the latest verison
Invoke-WebRequest -Uri $source -OutFile $dest
# Unzip the file
Expand-Archive -Path $dest -DestinationPath $location -Force
# Change to the root directory of the download
Set-Location $location\Virtual-Desktop-Optimization-Tool-main

# Remove Send Buffer Size
# The network reset breaks Packer
(Get-Content '.\Win10_VirtualDesktop_Optimize.ps1') -notmatch 'Set-NetAdapterAdvancedProperty -DisplayName "Send Buffer Size"' | `
    Set-Content '.\Win10_VirtualDesktop_Optimize.ps1'

# Run the powershell to apply the optimizations
.\Win10_VirtualDesktop_Optimize.ps1 -AcceptEULA -WindowsVersion 2009 -Verbose

Cheers
Dave