Unable to map Azure file share

I’ve been trying for days to figure out how to get an Azure file share mapped to the VM being built by packer. Everything I’ve found points to using New-PSDrive, but that has continued to fail.

This is, among many other things, what I’ve recently tried:

provisioner "powershell" {
    inline = [
      "cmd.exe /C \"cmdkey /add:`\"<storage_account>.file.core.windows.net`\" /user:`\"localhost\\<storage_account>`\" /pass:`\"<access_key>`\"\"",
      "New-PSDrive -Name F -PSProvider FileSystem -Root \\\\<storage_account>.file.core.windows.net\\fileshare -Persist -Scope Global"

No matter what I’ve attempted, I get

CMDKEY: Credentials cannot be saved from this logon session.


New-PSDrive : The network resource type is not correct

These commands work when I execute them locally in either Powershell 5.x or Powershell Core 7.x.

I’ve also tried defining credentials to pass to the New-PSDrive cmdlet. That also hasn’t worked.

I found my solution. I never got anything working with cmd.exe. Instead, I sent the storage account key through ConvertTo-SecureString and then created a System.Management.Automation.PSCredential object:

  provisioner "powershell" {
    # Continue configuration after restart  
    inline = [
      # Use the storage account variables to map the file share
      "$securePass = \"${var.storage_account_key}\" | ConvertTo-SecureString -AsPlainText -Force",
      "$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList \"${var.storage_account_name}\",$securePass",
      "New-PSDrive -Credential $creds -Name F -PSProvider FileSystem -Root \"${var.storage_path}\" >$null",


1 Like