Golden Image Creation - Azure File Share in Packer Provisioner

Hello,

I am trying to setup an Azure Virtual Desktop Golden Image that would contain our softwares pre-installed. The software packages are stored in a Azure File Share.
I am using ADO Pipeline and mapping the Azure File Share as a mapped drive. To invoke my packer build, here is a snippet of my PS script that I am calling from Packer provisioner:

$MYSHAREVARIABLE = "\\MYSTRGACC.file.core.windows.net\MYAFS"

$connectTestResult = Test-NetConnection -ComputerName MYSTRGACC.file.core.windows.net -Port 445
if ($connectTestResult.TcpTestSucceeded) {
  # Save the password so the drive will persist on reboot
  $no_out = cmd.exe /C "cmdkey /add:`"MYSTRGACC.file.core.windows.net`" /user:`"localhost\MYSTRGACC`" /pass:`"MYPASSKEY`""
  # Mount the drive
  $Z = Get-PSDrive "Z"-ErrorAction SilentlyContinue	
  if ($Z -eq $null) {
    # mount
    $no_out = New-PSDrive -Name Z -PSProvider FileSystem -Root $MYSHAREVARIABLE 
  }
  else {
    write-host "Existing drive Z: found."
  }
} 
else {
  Write-error -Message "Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port."
}
$Z = Get-PSDrive "Z"-ErrorAction SilentlyContinue
if ( $Z.Root -ne $MYSHAREVARIABLE ) {
  write-host "unable to bind drive Z to MYSHAREVARIABLE $MYSHAREVARIABLE "
  exit
}

When I run the same in ADO Pipeline, it is unable to find the share path. Here is a raw log extract.

2022-06-09T06:08:12.9760803Z ==> azure-arm.windows: Provisioning with Powershell…
2022-06-09T06:08:12.9767576Z ==> azure-arm.windows: Provisioning with powershell script: Install-RS2Client.ps1
2022-06-09T06:08:26.8754975Z azure-arm.windows:
2022-06-09T06:08:26.8766437Z azure-arm.windows:
2022-06-09T06:08:26.8769977Z ==> azure-arm.windows: Copy-Item : Cannot find path ‘Z:\Software\rs2server\rs2Client\rs2ClientStarter.exe’ because it does not exist.
2022-06-09T06:08:26.8776344Z azure-arm.windows: Directory: C:\Program Files
2022-06-09T06:08:26.8781954Z azure-arm.windows:
2022-06-09T06:08:26.8789594Z ==> azure-arm.windows: At C:\Windows\Temp\script-62a18e4c-9881-bb80-4139-db53667df9e8.ps1:47 char:1
2022-06-09T06:08:26.8793736Z azure-arm.windows:
2022-06-09T06:08:26.8798850Z azure-arm.windows: Mode LastWriteTime Length Name
2022-06-09T06:08:26.8805337Z ==> azure-arm.windows: + Copy-Item -Path "Z:\Software\rs2server\rs2Client\rs2ClientStarter.exe …
2022-06-09T06:08:26.8811366Z azure-arm.windows: ---- ------------- ------ ----
2022-06-09T06:08:26.8817162Z azure-arm.windows: d----- 6/9/2022 6:08 AM rs2
2022-06-09T06:08:26.8822457Z azure-arm.windows:
2022-06-09T06:08:26.8828480Z ==> azure-arm.windows: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-06-09T06:08:26.8834915Z azure-arm.windows:
2022-06-09T06:08:26.8839422Z azure-arm.windows: Directory: C:\Program Files\rs2
2022-06-09T06:08:26.8844903Z azure-arm.windows:
2022-06-09T06:08:26.8850287Z ==> azure-arm.windows: + CategoryInfo : ObjectNotFound: (Z:\Software\rs2…ientStarter.exe:String) [Copy-Item], ItemNotFoundExce
2022-06-09T06:08:26.8859499Z azure-arm.windows:
2022-06-09T06:08:26.8862285Z azure-arm.windows: Mode LastWriteTime Length Name
2022-06-09T06:08:26.8864116Z azure-arm.windows: ---- ------------- ------ ----
2022-06-09T06:08:26.8867679Z ==> azure-arm.windows: ption
2022-06-09T06:08:26.8872363Z azure-arm.windows: d----- 6/9/2022 6:08 AM rs2Client
2022-06-09T06:08:26.8878336Z ==> azure-arm.windows: + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
2022-06-09T06:08:26.8886205Z ==> azure-arm.windows:
2022-06-09T06:08:26.8894592Z ==> azure-arm.windows: Copy-Item : Cannot find path ‘Z:\Software\rs2server\rs2’ because it does not exist.
2022-06-09T06:08:26.8904407Z ==> azure-arm.windows: At C:\Windows\Temp\script-62a18e4c-9881-bb80-4139-db53667df9e8.ps1:48 char:1
2022-06-09T06:08:26.8906191Z ==> azure-arm.windows: + Copy-Item -Path “Z:\Software\rs2server\rs2” -Destination "c:\Program …
2022-06-09T06:08:26.8907395Z ==> azure-arm.windows: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

How do I overcome this? Any suggestions please?
Packer Version: 1.8.1

The issue has been resolved. It wasn’t with Packer, but with Powershell on the way variables were handled. Made some tweaks and my GI got created.