1

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 ?

KRM
  • 119
  • 1
  • 12

2 Answers2

0

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
KRM
  • 119
  • 1
  • 12
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Hey Charles, sorry for the late update. I was able to execute it successfully, I have updated my output in your answer, is it the supposed and right output ? Can you confirm ? – KRM May 26 '20 at 04:25
  • @Chaitanya Of course yes. Well, if it works for you please accept it as the answer. – Charles Xu May 26 '20 at 05:37
  • @CharlesXu, I can run any other scripts with Invoke-AzVMRunCommand , but Sysprep doesn't seem work. it got the same output as yours, but the VM was not shutdown. – jack.chen.job Dec 02 '22 at 22:00
0

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.

jack.chen.job
  • 199
  • 1
  • 7