Http response error: 401 - invalid content type while copying file on Azure Cloud Platform

Hi,

I’m using Builder and Provisioner to create Windows Image with a software install on it on Azure Cloud however build is getting failed while copying the file to VM being created. Error Message is: Build ‘azure-arm’ errored: Error uploading file to $env:TEMP\winrmcp-c3168c08-eeb0-4d5d-6130-6e0d16d68147.tmp: Couldn’t create shell: http response error: 401 - invalid content type. Any help would appreciate.

"builders": [{

    "type": "azure-arm",   
    "subscription_id": "<sub-id>",
    "managed_image_resource_group_name": "test-mansing",
    "managed_image_name": "New-Image-Create-By-Packer-Windows-withsoftware",    
    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "azure_tags": {
      "dept": "engineering"
    },
    "location": "West US",
    "vm_size": "Standard_A2",

    "communicator": "winrm",
    "winrm_use_ssl": "true",
    "winrm_insecure": "true",
    "winrm_timeout": "25m",
    "winrm_username": "packer"
}],
"provisioners": [{
  "type": "powershell",
    "inline": [
      " # NOTE: the following *3* lines are only needed if the you have installed the Guest Agent.",
      "  while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
      "  while ((Get-Service WindowsAzureTelemetryService).Status -ne 'Running') { Start-Sleep -s 5 }",
      "  while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",

      "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
      "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit /mode:vm",
      "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; Write-Output $imageState.ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Start-Sleep -s 10 } else { break } }"
  ]
},
{  "type": "file",
  "source": "StorageExplorer.exe",
  "destination": "C:/Temp/"
},
{  "type": "powershell",
  "inline": ["C:/Temp/StorageExplorer.exe /VERYSILENT /SUPPRESSMSGBOXES /NORESTART"]
}]}```

Even am facing similar issue. please do let me know incase if you found any work around

Hi @mansing2 the 401 error usually indicates a WinRM connection error. Looking at your provided template I can see that you are running the sysprep command in the provisioner before the file copy provisioner.

If you change the order of your provisioner so that the sysprep is the last command that gets executed does it resolve your issue?

Generally speaking the best practice around sysprep is to have it execute as the last provisioner in a build. Since it will usually change system security setting that prevent Packer from connecting to the machine.