Search code examples
azurepowershellazure-powershellpowershell-remotingazure-vm

How to run "sysprep /generalize” in Azure Windows Virtual Machine (VM) from my local machine using Powershell?


I have a windows Azure VM and need to execute “%windir%\system32\sysprep” and then execute “sysprep /generalize” both from admin mode from my local machine through Powershell. How can I do that ?


Solution

  • For your requirements, as I know you can use a PowerShell script to achieve it. First, you can take a look at the Sysprep, it can be run in a PowerShell command C:\WINDOWS\system32\sysprep\sysprep.exe /generalize /shutdown /oobe. Put this command inside a script, then you can use two ways to run this script in the VM from your local machine. One is that use the Invoke command.

    In Azure CLI:

    az vm run-command invoke --command-id RunPowerShellScript -g group_name -n vm_name --scripts @script.ps1
    

    In PowerShell:

    Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1'
    

    Another is that use the VM extension. It's a little complex. You can take a look at the Azure PowerShell command Set-AzVMCustomScriptExtension.

    Output after running:-

    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