Search code examples
azureazure-devopscontinuous-deploymentazure-virtual-machinesysprep

Sysprep an Azure VM using PowerShell task in a pipeline


My (dotNET) application is built (using a Windows Hosted agent), from a build pipeline, and in the subsequent Release pipeline, I provision a 16GB-Win2016 VM (enabling RDP, HTTP, HTTPS, WinRM and SSH), into which I RDP manually (there is a Manual Intervention task here), and configure WinRM (following this article: https://learn.microsoft.com/en-us/azure/marketplace/cloud-partner-portal/virtual-machine/cpp-configure-winrm-after-vm-creation#configure-vm-to-enable-winrm). Everything is fine until here. The next task is a Azure File Copy task, which essentially copies the Build artifacts (from $(System.DefaultWorkingDirectory)) and pastes into a directory I specify. Works like a charm. The next task I have is to create a VHD of this whole VM (essentially after the copying is done).

I know I can manually RDP into the VM (again) and sysprep (with oobe/generalize/shutdown), then maybe go back to the Azure Portal and Disk Export the OS Disk (specifying the SAS URL expiration time at whatever (36000 per the article)) BUT can this all be automated?

So, long story short - I'd like to know if sysprep oobe/generalize/shutdown can be performed remotely preferably over a PS task. I understand the other part of it (exporting the disk and all) can be, but if sysprep can be done remotely nothing like it.


Solution

  • I tried this and got what I wanted:

    $sysprep= 'C:\Windows\System32\Sysprep\Sysprep.exe'
    $arg1 = '/generalize'
    $arg2 = '/oobe'
    $arg3 = '/shutdown'
    $arg4 = '/quiet'
    
    & $sysprep $arg1 $arg2 $arg3 $arg4 -Wait