PowerCLI

 View Only
Expand all | Collapse all

Script to shutdown and remove list of vms?

  • 1.  Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 02:33 PM

    Hello

    I would like to use powercli to shutdown and remove a list of virtual machines from a .csv file.  Althought not entirely clear on how to do it, can someone help me out?



  • 2.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 03:00 PM

    Hi, there,

    Can you provide the format of the csv file? An example would be good.

    How do you like to filter them ? Which VMs are going to be removed and shut down ?

    Other than deserialization of the csv file to .net objects, it's easy to write such as scirrpt:

    $vms =  <deserialize-from-custom-csv>

    $filteredVMs = $vms |  ?{ <condition>}

    you have 2 approaches here:

    $filteredVMs |  Shutdown-VMGuest -confirm:$false

    or

    $filteredVMs |  Stop-VMt -confirm:$false

    Then I suppose you would want to serialize to CSV again

    you can do the following:

    $remainingVMs = @()

    foreach( $vm in $vms){

        if($filteredVms -notcontains $vm){
              $remainingVMs += $vm
        }

    }

    Export-CSV -Path <your-file-here> -InputObject $remainingVMs

    If you have trouble in finished the script, don't hesitate to ask.

    Best regards,

    Leni Kirilov

    PowerCLI Team

    Export-CSV


  • 3.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 03:04 PM

    Hi Leni

    I don't have the .csv file as of yet because I dont know what it is I need to include in it.  I would like to just populate the vmname and go from there but not sure if I need to specify the DS or host to accomplish this.

    Do you know?



  • 4.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 03:08 PM

    Hi, there,

    Ok, in that case explain in more details your concrete case. What it is you want to achieve?

    - you want to store some VMs in a csv and then use it to shutdown/stop vms ?

    - you want to write manually some VM names and IP of their hosts and then use it to extract them and do the same stuff to them?

    I can't tell you how the csv looks, because you define it based on your needs.

    In either case it's easy to initialize VM objects on which you can use Star-Vm/Stop-Vm etc

    Best regards,

    Leni Kirilov

    PowerCLI Team



  • 5.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 03:15 PM

    I want to be able to add a list of vmnames to a .csv file and run a script that will shut them down and remove them permanently.

    That list will be updated/editied as the need arises to remove more and more vms.

    like so:

    remove.csv

    Server Name
    w2k3sp2s32qa7-vm
    w2k8sp2s32qa1-vm
    w2k8sp2s64qa8-vm
    w2k8sp2s64qa9-vm
    w3r232-100
    w3r232-121
    w3r232-122


  • 6.  RE: Script to shutdown and remove list of vms?
    Best Answer

    Posted Sep 09, 2011 03:50 PM

    Hello, max08-

    To just use that CSV as a list of VM names to stop and remove permanently, you can do something like:

    Import-Csv remove.csv | %{Stop-VM $_."Server Name" -Confirm:$false | Remove-VM -DeletePermanently -Confirm:$false -RunAsync}

    Note, that is non-interactive once you run it -- it does not prompt to stop the VMs or to delete them from disk, it just does it.  So, be sure that the .csv file has only names of VMs that you really want to kill.

    How does that do for you?



  • 7.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 03:59 PM

    Awesome, thanks Matt!   Worked like a charm.



  • 8.  RE: Script to shutdown and remove list of vms?

    Posted Sep 09, 2011 04:20 PM

    One more question, can I check to see if the VM is powered on?  If its powered off already it doesn't remove the VM as it attempts to power it off and then skips the removing it IF its already off.



  • 9.  RE: Script to shutdown and remove list of vms?

    Posted Sep 10, 2011 01:39 AM

    Hello, max08-

    Yes, that would be a matter of adding a piece to check for the VM's power state.  Like:

    Import-Csv remove.csv | %{
       
    $vmTmp = Get-VM $_."Server Name"
       
    ## if the VM is on, power it off
        if ($vmTmp.PowerState -eq "PoweredOn") {Stop-VM $vmTmp -Confirm:$false}
       
    Remove-VM -VM $vmTmp -DeletePermanently -Confirm:$false -RunAsync
    }
    ## end foreach-object

    Enjoy.



  • 10.  RE: Script to shutdown and remove list of vms?

    Posted Sep 13, 2011 02:37 PM

    Thanks Matt.  One more thing, can we make the script check for that vm before it does anything, if it doesnt find the name then just move on to the next one?  It does it already but it prints an error of "ObjectNotfound".  Can we just skip over that message all together?



  • 11.  RE: Script to shutdown and remove list of vms?

    Posted Sep 14, 2011 04:09 AM

    Hello again-

    Yes, sure can.  By using the -ErrorAction parameter on Get-VM, there will be no error message for when a VM is not found.  Then, after testing to see if $vmTmp is non-null, the script will go through the power down and removal as before.  Like:

    Import-Csv remove.csv | %{
       
    $vmTmp = Get-VM $_."Server Name" -ErrorAction SilentlyContinue
       
    if ($vmTmp) {
           
    ## if the VM is on, power it off
            if ($vmTmp.PowerState -eq "PoweredOn") {Stop-VM $vmTmp -Confirm:$false}
           
    Remove-VM -VM $vmTmp -DeletePermanently -Confirm:$false -RunAsync
        }
    ## end if
        else {<# ...do nothing -- no VM by this name #>}
    }
    ## end foreach-object

    Enjoy



  • 12.  RE: Script to shutdown and remove list of vms?

    Posted Oct 17, 2011 06:00 PM

    Thanks for your help Matt.

    Trying to do the reserve now with simply powering on a list of VMs if they are powered off.

    Import-Csv poweron.csv | %{
        $vmTmp = Get-VM $_."Server Name" -ErrorAction SilentlyContinue
        if ($vmTmp) {
        Start-VM $vmTmp -Confirm:$false -RunAsync
        } ## end if
        else {<# ...do nothing -- no VM by this name #>}
    } ## end foreach-object

    Have an issue with listing the failed attempts because the VM is already powered on.  Can you help me out?



  • 13.  RE: Script to shutdown and remove list of vms?

    Posted Oct 17, 2011 10:29 PM

    Hello, max08-

    You can check the power state in the If statement, and only try to power on the VM if it is "PoweredOff".  Like:

    Import-Csv poweron.csv | %{
       
    $vmTmp = Get-VM $_."Server Name" -ErrorAction SilentlyContinue
       
    if ($vmTmp.PowerState -eq "PoweredOff") {
           
    Start-VM $vmTmp -Confirm:$false -RunAsync
        }
    ## end if
        else {<# ...do nothing -- no powered off VM by this name #>}
    }
    ## end foreach-object

    Enjoy