VMware Cloud Community
elschme
Contributor
Contributor
Jump to solution

Remove-vm with vSphere 5 don't work

Hello,

I am trying to delete a vm which I created thru vCenter before.

This fails with a error message:

Remove-VM : 26.03.2012 09:16:09    Remove-VM        The operation for the entity VirtualMachine-vm-1709 failed wi
th the following message: "The object has already been deleted or has not been completely created"   
Bei C:\Users\elschme\Documents\DeleteVMScript.ps1:15 Zeichen:10
+ Remove-VM <<<<  $_ -DeletePermanently -Confirm:$false
     + CategoryInfo          : NotSpecified: (:) [Remove-VM], ManagedObjectNotFound
     + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAuto
    mation.ViCore.Cmdlets.Commands.RemoveVM

This is my script:

$MyHosts = "de-mdi-vmhost1", "de-mdi-vmhost2"
$username = "user"
$password = "pass"
$vm2delete = "test_klon"

Connect-VIServer 10.182.50.180 -user root -password vmware

$MyHosts | Foreach {
Write-Host "Connecting to host: $($_)"
$connection = Connect-VIServer $_ -User $username -Password $password
}

$vm2delete | Foreach {
Write-Host "Deleting host: $($_)"
Remove-VM $_ -DeletePermanently -Confirm:$false
}

Can anyone help me with this please?

Regards.

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I wonder why you connect to the vCenter server and to the hosts? In my opinion only connecting to the vCenter server would be sufficient. You might get the error because when the VM is removed via the vCenter server it can't be removed via the host anymore. Or the other way around. Try removing the following code from your script:


$MyHosts | Foreach {
Write-Host "Connecting to host: $($_)"
$connection = Connect-VIServer $_ -User $username -Password $password
}

Besides the above the line:

Write-Host "Deleting host: $($_)"

should probably read:

Write-Host "Deleting VM: $($_)"

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

It looks as if that VM is in a funny state.

Can you try with another VM ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I wonder why you connect to the vCenter server and to the hosts? In my opinion only connecting to the vCenter server would be sufficient. You might get the error because when the VM is removed via the vCenter server it can't be removed via the host anymore. Or the other way around. Try removing the following code from your script:


$MyHosts | Foreach {
Write-Host "Connecting to host: $($_)"
$connection = Connect-VIServer $_ -User $username -Password $password
}

Besides the above the line:

Write-Host "Deleting host: $($_)"

should probably read:

Write-Host "Deleting VM: $($_)"

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
elschme
Contributor
Contributor
Jump to solution

Thank you Robert. This was the problem.

Regards,

Leo.

0 Kudos