Golden Image Creation - Azure File Share in Packer Provisioner


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:


$connectTestResult = Test-NetConnection -ComputerName -Port 445
if ($connectTestResult.TcpTestSucceeded) {
  # Save the password so the drive will persist on reboot
  $no_out = cmd.exe /C "cmdkey /add:`"`" /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 "

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

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.