VM Created from Image with Azure Powershell does not set ComputerName

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?

Apr 1, 2022 in Azure by Edureka
• 13,620 points
787 views

1 answer to this question.

0 votes

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

  1. Create some variables.

    $vmName = "myVM" $rgName = "myResourceGroup" $location = "EastUS" $imageName = "myImage"
  2. Get the VM.

         $vm = Get-AzVm -Name $vmName -ResourceGroupName $rgName

3. Get the ID of the managed disk.

$diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id

Create the image configuration.

$imageConfig = New-AzImageConfig -Location $location $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $diskID

4. Create the image

New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig

Learn how to move an Azure VM image to another resource group here.

answered Apr 1, 2022 by Edureka
• 12,690 points

Related Questions In Azure

0 votes
0 answers

C# NuGet package error in Azure DevOps with image

I'm creating a NuGet package to distribute ...READ MORE

Apr 12, 2022 in Azure by Edureka
• 13,620 points
559 views
0 votes
1 answer

Azure Search synonyms not reflecting in results

You most certainly should. If the words ...READ MORE

answered Mar 2, 2022 in Azure by Edureka
• 12,690 points
873 views
0 votes
1 answer

Difference between Azure CLI and PowerShell

The Azure CLI comes with an installer ...READ MORE

answered Mar 25, 2022 in Azure by Edureka
• 13,620 points
1,870 views
0 votes
1 answer

Fetch AzureVm name from private IP in powershell

If by a better way you mean ...READ MORE

answered Jul 17, 2018 in DevOps on Cloud by DareDev
• 6,890 points
1,917 views
0 votes
1 answer
0 votes
1 answer

Azure Advisor Recommendations using PowerShell

The command appears to be correct. I ...READ MORE

answered Mar 4, 2022 in Azure by Edureka
• 13,620 points
1,319 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP