Azure-ARM Packer powershell provisioner doesn't work for me

Hello, I’m trying to use packer to build a windows image on azure.
Copying files and setting registry entries work fine except for the huge amount of time copying files over winrm.
However if I want to change something on the OS like installing powershell modules or Windows Features powershell simply hangs and no output is provided. Already tried with elevated_user and the result is the same.
Also tried multiple versions of packer and the result is always the same.

==> azure-arm: Pausing before the next provisioner . Press enter to continue.
2020-08-14T09:54:36+01:00: ==> azure-arm: Provisioning with Powershell…
2020-08-14T09:54:36+01:00: ==> azure-arm: Provisioning with powershell script: C:\Users\z003yu2b\AppData\Local\Temp\powershell-provisioner929808231
2020-08-14T09:54:42+01:00: azure-arm: Creating folder Scripts
2020-08-14T09:54:42+01:00: azure-arm:
2020-08-14T09:54:42+01:00: azure-arm:
2020-08-14T09:54:42+01:00: azure-arm: Directory: C:
2020-08-14T09:54:42+01:00: azure-arm:
2020-08-14T09:54:42+01:00: azure-arm:
2020-08-14T09:54:42+01:00: azure-arm: Mode LastWriteTime Length Name
2020-08-14T09:54:42+01:00: azure-arm: ---- ------------- ------ ----
2020-08-14T09:54:42+01:00: azure-arm: d----- 8/14/2020 8:54 AM Scripts
2020-08-14T09:54:42+01:00: azure-arm: Creating folder BuildAgents
2020-08-14T09:54:42+01:00: azure-arm: d----- 8/14/2020 8:54 AM BuildAgents
2020-08-14T09:54:42+01:00: azure-arm: Creating folder Binaries
2020-08-14T09:54:42+01:00: azure-arm: d----- 8/14/2020 8:54 AM Binaries
2020-08-14T09:54:42+01:00: azure-arm: Installing Module AZ

Stays in the last step forever.

Code:

{

"builders": [{

  "type": "azure-arm",

  "client_id": "XXXXXXXXXXXXXX",

  "client_secret": "XXXXXXXXXXXXXXXXXXXX",

  "tenant_id": "XXXXXXXXXXXXXXXXXX",

  "subscription_id": "XXXXXXXXXXXXXXXXXXXX",

  "managed_image_resource_group_name": "XXXXXXXX",

  "managed_image_name": "buildserver",

  "os_type": "Windows",

  "image_publisher": "MicrosoftWindowsServer",

  "image_offer": "WindowsServer",

  "image_sku": "2016-Datacenter",

  "communicator": "winrm",

  "winrm_use_ssl": true,

  "winrm_insecure": true,

  "winrm_timeout": "5m",

  "winrm_username": "packer",

  "azure_tags": {

      "dept": "Engineering",

      "task": "Image deployment"

  },

  "build_resource_group_name": "XXXXXX,

  "vm_size": "Standard_D2_v2"

}],

"provisioners": [

    {

        "type": "powershell",

        "inline":[

            "Write-Host \"set strong cryptography on 64 bit .Net Framework (version 4 and above)\"",

            "Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord",

            "Write-Host \"set strong cryptography on 32 bit .Net Framework (version 4 and above)\"",

            "Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord" 

        ]

    },

    {

        "type": "windows-restart"

    },

    {

        "type": "powershell",

        "inline":[

            "Write-Host \"Creating folder Scripts\"",

            "New-Item -ItemType Directory -Name Scripts -path C:\\",

            "Write-Host \"Creating folder BuildAgents\"",

            "New-Item -ItemType Directory -Name BuildAgents -path C:\\",

            "Write-Host \"Creating folder Binaries\"",

            "New-Item -ItemType Directory -Name Binaries -path C:\\",

            "Write-Host \"Installing Module AZ\"",

            "Install-Module Az -AllowClobber -Force"

        ]

    },

    {

        "type": "file",

        "source": ".\\Scripts\\ipins-windows-installation-master.zip",

        "destination": "C:\\Scripts\\"

    },

    {

        "type": "file",

        "source": ".\\Scripts\\trendmicro-windows-installation-master.zip",

        "destination": "C:\\Scripts\\"

    },

    {

        "type": "file",

        "source": ".\\Tests\\image.tester.ps1",

        "destination": "C:\\Scripts\\"

    },

    {

        "type": "powershell",

        "inline":[

            "Invoke-Pester C:\\Scripts\\image.tester.ps1 -OutputFile TestResults.xml -OutputFormat NUnitXml"

        ]

    },

    {

        "type": "file",

        "source": "TestResults.xml",

        "destination": "TestResults.xml",

        "direction": "download"

    }

]

}

Is internet access allowed for the VM, i.e. no NSG’s on the subnet or route table changes and any VM on that network can get direct internet access?

I am having same problem with powershell module. Tried various things, nothing helped. Looks like I’ll do it in ansible provisioner, no other way…

Possible issue is due to how the elevated_user works internally, that is it creates scheduled task. Here is detailed explanation of the issue and the last post in the thread presents how to work around it with Windows restart and, perhaps optional, auto-logon the user used by Packer back:

It seems to be a known issue Issue with running PowerShell inline script with elevated privileges · Issue #7729 · hashicorp/packer · GitHub and possibly still unresolved, and the Packer documentation is not providing any explanation for end users.