VMware Cloud Community
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Stop VM and remove from Inventory

Hi Guys,

I need to script stoping VMs only if powered on and remove from inventory, I need to do this from a csv list with VMnames, can anyone please help?

Nicholas
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, -

Should be pretty straightforward.  Assuming that you are saying, "delete all of the VMs in this CSV, first powering off any that are in poweredOn state", you could using something like:

## CSV has column VMName

Import-Csv -Path c:\temp\myVMsToDelete.csv | ForEach-Object {

    $oThisVM = Get-VM -Name $_.VMName

    if ($oThisVM.PowerState -eq "PoweredOn") {Stop-VM -Confirm:$true -VM $oThisVM}

    $oThisVM | Remove-VM -DeletePermanently -Confirm:$true -RunAsync

} ## end foreach-object

Note:  for the sake of not putting some code here that might accidentally get copied/pasted and cause havoc, I put $true as the -Confirm value instead of $false.  Presumably you might want to have this run without confirming each power-off and deletion.  If so, change those $true values to $false (two occurrences).

How does that do for you?

View solution in original post

0 Kudos
3 Replies
mattboren
Expert
Expert
Jump to solution

Hello, -

Should be pretty straightforward.  Assuming that you are saying, "delete all of the VMs in this CSV, first powering off any that are in poweredOn state", you could using something like:

## CSV has column VMName

Import-Csv -Path c:\temp\myVMsToDelete.csv | ForEach-Object {

    $oThisVM = Get-VM -Name $_.VMName

    if ($oThisVM.PowerState -eq "PoweredOn") {Stop-VM -Confirm:$true -VM $oThisVM}

    $oThisVM | Remove-VM -DeletePermanently -Confirm:$true -RunAsync

} ## end foreach-object

Note:  for the sake of not putting some code here that might accidentally get copied/pasted and cause havoc, I put $true as the -Confirm value instead of $false.  Presumably you might want to have this run without confirming each power-off and deletion.  If so, change those $true values to $false (two occurrences).

How does that do for you?

0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Mattboren,

Thanks for this I actually came up with this something similar but yours seems more streamlined also I don't want to delete the VM just remove from inventory. The only real issue I have is if for some reason the CSV lists vmname that doesn't exist the whole script fails can you help with tuning of this?

$csv = import-csv c:\vmlabs\csv\stop-remove-vm.csv

ForEach ($row in $csv){

$vm = get-vm $row.vmname

if($vm.powerstate -like "PoweredOn"){

Stop-VM -VM $vm -Confirm:$false -RunAsync | Out-Null}

}

Start-Sleep -Seconds 15

ForEach ($row in $csv) {

$vm = get-vm $row.vmname

Remove-VM -VM $vm -Confirm:$false -RunAsync | Out-Null}

Nicholas
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello-

Sure thing.  Oh, great, fine job.

Yes, we can just add another conditional statement and also use the ErrorAction parameter on Get-VM to continue on when a VM is not found.  Below is some updated code that will do such things.  I also set the DeletePermanently parameter value to $false, so as to just unregister the VMs instead of deleting them from disk, as you said that is your need.

## CSV has column VMName

Import-Csv -Path c:\temp\myVMsToDelete.csv | ForEach-Object {

    ## if there is such a VM in this vCenter, do something

    if ($oThisVM = Get-VM -Name $_.VMName -ErrorAction:SilentlyContinue) {

        if ($oThisVM.PowerState -eq "PoweredOn") {Stop-VM -Confirm:$true -VM $oThisVM}

        $oThisVM | Remove-VM -DeletePermanently:$false -Confirm:$true -RunAsync

    } ## end if

    else {Write-Verbose -Verbose "No VM of name '$($_.VMName)' found. Continuing"}

} ## end foreach-object 

I imagine that you can see how to update your two-pass code in a similar way.  Or, this code should also succeed for you (it is still set to Confirm as written -- change that as desired).  How is it?

0 Kudos