Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Use a Custom Script Extension to Sysprep an Azure VM

$
0
0

Hi All,

I’ve been in a situation where I want to run sysprep on a Azure VM to create an image, hopefully without needing to login / connect directly to the VM. I thought that this was an opportunity to learn about custom script extensions. (for use in other situations as well)

 

in summary, I created a powershell script file, and saved it to a new file ‘sysprep.ps1’:

param([switch]$runSysprep=$false)
write-output "Sysprep Script Run, parameter 'runSysprep': $runSysprep"

if($runSysprep){
  write-output "starting Sysprep"
  Start-Process -FilePath C:WindowsSystem32SysprepSysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
  write-output "started Sysprep"
}else{
  write-output "skipping Sysprep"
}

 

I then uploaded this to a storage account, set the security on the container to ‘blob’ (there’s nothing sensitive in that script), then ran the following:

$VMName =  'vmName'
$VMRG = 'vmResourceGroup'
$VMLocation = 'AustraliaEast'
$ExtensionName = 'runsysprep'
$Scripturi = 'https://<storageAccountName>.blob.core.windows.net/templates/scripts/sysprep.ps1'
Set-AzureRmVMCustomScriptExtension -FileUri $ScriptURI -ResourceGroupName  $VMRG -VMName $VMName -Name $ExtensionName -Location $VMLocation -run './scripts/sysprep.ps1' -Argument '-runSysprep'

 

This gave the output:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

To see the output messages from the script that was run:

$status = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $VMRG -VMName $VMName -Name $ExtensionName -Status
$status.SubStatuses.message

This displayed the output that was expected:

Sysprep Script Run, parameter 'runSysprep': Truenstarting Sysprepnstarted Sysprep

 

A short time later, the VM is shutdown (but is still allocated & incurring charges), so i then shutdown the VM:

Stop-AzureRmVM -Name $VMName -ResourceGroupName $VMRG  -force

 

As with everything I post about, please give comments / questions on my approach /methods

Nick


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>