Add-VMNewHardDisk is not working for me today when it did a week ago

Mar 11, 2009 at 5:38 PM
I must have a setting incorrect, but Add-VMNewHardDisk is failing inside the Add-VMDisk function, specifically on "$result=$VSMgtSvc.psbase.invokeMethod("AddVirtualSystemResources", $arguments)." I receive a 4096 error. This same code was working for me last week. I have not changed anything that I can recall.

Here are the steps I am using. I am creating a differencing disk.

Thanks for the help.

$server = "myserver"
$vmName = "TestChild"
$vmParent = "TestParent"
$vmVirtualSwitch = "My Virtual Network"

# create a new virtual machine
$vm = New-VM $vmName -server $server

# get the vm
#$vm = Get-VM $vmName

# set cpu count
Set-VMCPUCount $vm 2 -server $server

# set memory size
Set-VMMemory $vm 2 -memory 4GB -server $server

# add a legacy network adapter
Add-VMNIC -vm $vm -VirtualSwitch $vmVirtualSwitch -legacy

# add a default VMBus (non-legacy) network adapter
Add-VMNIC -vm $vm -VirtualSwitch $vmVirtualSwitch

# add the hard drive to the VM - DIFFERENCING DISK
$vhdPath = "$(Get-VhdDefaultPath)\$vmName.vhd"
$vhdParent = "$(Get-VhdDefaultPath)\$vmParent.vhd"

Add-VMNewHardDisk –vm $vm -controllerID 0 -lun 0 -vhdPath $vhdPath –parent $vhdParent -size 20GB

Mar 11, 2009 at 6:00 PM
Sorry to waste your time. I got this working after changing my $vmParent to equal "TestParentVM," which is the name of the actual parent. So my 4096 error was because it could not find the proper parent. I wish those return results would show more detail.

Thanks.
Mar 11, 2009 at 7:08 PM
One more thing to add, then I will get off my soap box for the day. I have managed to carry on a conversation with just myself with this thread. I hope it helps someone in the future. I have noticed that adding a new vm hard disk fails intermittently.

Add-VMNewHardDisk –vm $vm -controllerID 0 -lun 0 -vhdpath $vhdPath -parent $parent

As a result, I am putting this into a while loop and checking to see if the disk has been added after I execute the above code. If the disk has not been added, try and add it again. I have found success with this. I am not sure why the disk fails to add, but I get those 4096 errors. Here is the code I am using for now.

# add the hard drive to the VM
$parent = "$(get-VHDdefaultPath)\$vmParent.vhd"
$vhdPath = "$(get-VHDdefaultPath)\$vmName.vhd"
Add-VMNewHardDisk –vm $vm -controllerID 0 -lun 0 -vhdpath $vhdPath -parent $parent

# get the VM to see if a disk is attached to it, if it is not, then it failed
$disks = Get-VMDisk -vm $vm
$count = 1

# if the disk was not successfully added, try and add it again
while ($disks -eq $null -and $count -le 5)
{
 Write-Host "RETRY $count - The disk was not properly added. Attempting to retry."
 
 # delete the disk that was added but not attached
 if (test-path -path $vhdPath)
 {
  Remove-Item -Path $vhdPath -Force
 }
 
 Add-VMNewHardDisk –vm $vm -controllerID 0 -lun 0 -vhdpath $vhdPath -parent $parent
  
 $disks = Get-VMDisk -vm $vm
 $count++
}

if ($disks -eq $null)
{
 Write-Host "Operation failed to create this virtual machine. Contact an admin."
}
else
{
 Write-Host "Operation completed successfully. Attempting to start the VM."
 Start-VM -vm $vm -wait
}