New-PSDrive in Packer Azure template fails

45 Views Asked by At

I've been trying to map a network drive when I run a Packer template but it keeps failing.

provisioner "powershell" {
  inline = [
    "New-PSDrive -Name F -PSProvider FileSystem -Persist -Root \\\\<storage-account-path>\\<share>\\"
  ]
}

Another person had the same issue, but Packer wasn't involved.

Based on the answer provided, I tried different formats. I get one of two errors depending on the format of the path I'm mapping. I'm using the -Persist option to use scripts and files on the share in further Powershell provisioners.

If I use a trailing \ on the path I get

+ New-PSDrive -Name F -PSProvider FileSystem -Persist -Root \\stgacct ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (F:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand 

If I don't use a trailing \ I get

New-PSDrive : The network resource type is not correct

According to the same answer the issue was fixed in early versions of Powershell Core 7 but likely isn't getting fixed in Powershell 5.x. I'm assuming that when Packer runs inline Powershell commands it is executing on v5.x (Is that accurate?)

I don't know if that is relevant, though, because if I run the New-PSDrive command in a 5.x session on my workstation it succeeds. It also works if I use a script instead of inline commands.

provisioner "powershell" {
  script = "C:\\Users\\User1\\Development\\map_share.ps1"
}

Script:

$connectTestResult = Test-NetConnection -ComputerName <storage-account-path> -Port 445
if ($connectTestResult.TcpTestSucceeded) {
    # Mount the drive
    New-PSDrive -Name W -PSProvider FileSystem -Root \\<storage-account-path>\file-share -Persist
} 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."
}

Another post purports to have a solution, but I haven't found anything to corroborate.

The network share that I'm trying to map doesn't require authentication.

0

There are 0 best solutions below