Genrealize (sysprep) an Azure Virtual Machine via Runbook Script

Joseph Durnal 321 Reputation points
2020-10-06T18:24:29.667+00:00

I'm working on a runbook to automate the process of capturing an image from a master VM and deploying it to a shared image gallery. The one part I can't seem to figure out is how to generalize the source disk before capturing it.

I was thinking something like this would be the way but it didn't work as expected:

Invoke-AzVMRunCommand -ResourceGroupName wvd -Name wvd-capture -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Windows\System32\Sysprep\Sysprep.exe' -Parameter @{"arg1" = "/oobe";"arg2" = "/generalize";"arg3" = "/shutdown";"arg4" = "/quiet"}

Any thoughts on how I can do this?

Thanks

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,981 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,095 questions
{count} votes

Accepted answer
  1. Joseph Durnal 321 Reputation points
    2020-10-09T17:43:38.713+00:00

    I finally got a good answer to this, than's to r/azure on reddit. I had trouble with some of the answers proposed here, not that they weren't good suggestions, just didn't seem to work for me.

    $vm = Get-AzVm -ResourceGroupName 'wvd' -Name 'wvd-capture'
    
    $scriptContent = @'
        Start-Process -FilePath C:\Windows\System32\Sysprep\Sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
    '@
    
    $execScriptName = 'Invoke-TempScript.ps1'
    $execScriptPath = New-Item -Path $execScriptName -ItemType File -Force -Value $scriptContent | select -Expand FullName
    
    $invokeParams = @{
        VM         = $vm
        CommandId  = 'RunPowerShellScript'
        ScriptPath = $execScriptPath
    }
    $result = Invoke-AzVMRunCommand @invokeParams
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2020-10-07T21:41:23.733+00:00

    Maybe this is helpful?

    https://tech.xenit.se/azure-automation-running-scripts-locally-vm-runbooks/

    Regards
    Andreas Baumgarten

    (Please don't forget to Accept as answer if the reply is helpful)

    1 person found this answer helpful.
    0 comments No comments

  2. KarishmaTiwari-MSFT 17,997 Reputation points Microsoft Employee
    2020-10-08T03:11:38.54+00:00

    Here is a document on Run scripts in your Windows VM with some options on running scripts in your windows VM. Please take a look at the Hybrid runbook worker to see if it helps in your scenario.

    As you mentioned, you could look at using Azure Image Builder (AIB) if it fits your scenario (Please note AIB is still in preview). For AIB, you have to specify a source such as a marketplace image or a custom image. It is not able to utilize a currently running VM. AIB will build its own VM, run whatever scripts you specify and run sysprep automatically before capturing the image and then distribute to either a VHD, managed image, or to Shared Image Gallery.

    1 person found this answer helpful.
    0 comments No comments

  3. FENG CHEN 16 Reputation points
    2022-12-02T21:55:37.637+00:00

    hmm, I can run other scripts, but sysprep doesn't seem work for me ( on a Windows 10 VM).

    I have a 'PowershellScripts/RunSysPrep.ps1' with content:

    Start-Process -FilePath C:\Windows\System32\Sysprep\Sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'

    When I run :
    Invoke-AzVMRunCommand -ResourceGroupName $vmRG -VMName $tempVMName -CommandId 'RunPowerShellScript' -ScriptPath 'PowershellScripts/RunSysPrep.ps1'

    result:

    Value[0] :
    Code : ComponentStatus/StdOut/succeeded
    Level : Info
    DisplayStatus : Provisioning succeeded
    Message :
    Value[1] :
    Code : ComponentStatus/StdErr/succeeded
    Level : Info
    DisplayStatus : Provisioning succeeded
    Message :
    Status : Succeeded
    Capacity : 0
    Count : 0

    But the VM is not shutdown, Boot diagnostics shows VM time keep updated.

    @Joseph Durnal , did sysprep work for you ( the VM was generalized and shutdown) ?

    Thanks,

    0 comments No comments

  4. FENG CHEN 16 Reputation points
    2022-12-02T22:10:24.313+00:00

    I could't make sysprep work with Invoke-AzVMRunCommand, It run with succeeded status, but the VM was not shutdown.

    Finally found https://developercommunity.visualstudio.com/t/devops-sysprep-public-agents/1375989 and it make sense.

    So just use Invoke-AzVMRunCommand to run sysprep won't work, I am thinking to reset a local admin user password and run the process as local admin might be a workaround.