0
votes

I have an Azure Image, which when I use Azure Powershell to create a VM from, despite me setting the ComputerName in the script, the VM is created without setting the ComputerName to the provided value.

Script:

$ImageName = 'MyImage'
$RsgName = 'MyRsg'
$VmName = 'MyNewVM'
$DiagnosticStorageName = 'diagnosticsstore5048'

$cred = Get-Credential -Credential 'TheAdmin'

$Image = Get-AzureRmImage -ImageName $ImageName -ResourceGroupName $RsgName

# Get NIC
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $RsgName

# Configure the new VM
$Vm = New-AzureRmVMConfig -VMName $VmName -VMSize 'Standard_A2_v2'
$Vm = Set-AzureRmVMSourceImage -VM $Vm -Id $Image.Id
$Vm = Set-AzureRmVMOSDisk -VM $Vm -Name $VmName'-disk' -StorageAccountType 'StandardLRS' -DiskSizeInGB '128' -CreateOption FromImage -Caching ReadWrite
$Vm = Add-AzureRmVMNetworkInterface -VM $Vm -Id $nic.Id
$Vm = Set-AzureRmVMBootDiagnostics -VM $Vm -Enable -ResourceGroupName $RsgName -StorageAccountName $DiagnosticStorageName
$Vm = Set-AzureRmVMOperatingSystem -VM $Vm -Windows -ComputerName 'dor' -Credential $Cred -ProvisionVMAgent

New-AzureRmVM -VM $Vm -ResourceGroupName $RsgName -Location 'West Europe' -DisableBginfoExtension

Last time I run the script to create the VM, it left the new VM with a computer name of 'WIN-I80O6J22ENS' The Image was created as per the process here: https://docs.microsoft.com/en-gb/azure/virtual-machines/windows/capture-image-resource?toc=%2Fazure%2Fvirtual-machines%2Fwindows%2Fclassic%2Ftoc.json

UPDATE Alot of people think that I am not Generalizing the image correctly, so I wanted to add here how I am doing it. Inside the VM I run:

Start-Process -FilePath $env:windir\System32\Sysprep\Sysprep.exe -ArgumentList "/generalize /oobe /shutdown /unattend:$env:windir\System32\Sysprep\unattend.xml"

Unattend.xml only has one setting in it which is to step the TimeZone.

Once this is completed and the VM OS has shut down, I run the following to get the VM, stop it, and set it to Generalized:

# Shutdown Source VM & Generalize
$SourceVM = Get-AzureRmVM -ResourceGroupName $SourceRsg -Name $SourceVMName
$Null = Stop-AzureRMVM -ResourceGroupName $SourceRsg -Name $SourceVMName -Force
Write-Host 'Stopped Source VM'

Set-AzureRmVm -ResourceGroupName $SourceRsg -Name $SourceVMName -Generalized
Write-Host 'Set Source VM to Generalized'

I have noticed that when the last command is ran, the output is:

OperationId :
Status :
StartTime :
EndTime :
Error :

It doesn't actually say if it was successful or not?

After this I create the Image from the VM disk.

3

3 Answers

0
votes

It might be caused by the image not being "Generalized".

Take a look at the Create a managed image of a generalized VM in Azure guide.

Hope it helps!

0
votes

If you have a specialized image, the Computer name will be missing because the old computer name and the new computer name share the same RID and SID number and on a ideal situation each virtual machine deployment has different RID and SID number. The Computer name can be displayed if the image is a generalized.

Check this article for more information:

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/create-vm-specialized

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/capture-image-resource

0
votes

The point everyone is trying to make, from your description of the issue, it seems the Image was not generalized correctly or aspect of the generalization failed.

The script work fine on my end, so the only issue is with the image. Screenshot

Try another deployment from that image an see is you get the same computer name (WIN-I80O6J22ENS).

If you get the same name that is a sure confirmation of issue with generalization.

If the name changes it might mean that the machine you imaged has a policy that is not accepting the name 'dor' so the system is generating default name.

Either way issue is not with the Powershell or Azure. Hope this helps.