PowerCLI

 View Only
Expand all | Collapse all

get-vm -name vm1-5

  • 1.  get-vm -name vm1-5

    Posted Feb 14, 2012 11:46 PM

    how can i get a list of my vms listed as

    vm1-5

    and shut them down

    one liner can do?

    get-vm -name vm1-5 | shutdown-vmguest



  • 2.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 12:05 AM

    you can use the range operator.  There are other ways to use range operators but this should demonstrate it to your needs.

    $range = 1..5

    foreach($name in $range){

    $vmname = "vm$name"

    }

    More simply stated - where % means foreach

    $vms = 1..5 | %{"vm$_"}

    $vms will then output

    vm1

    vm2

    vm3

    vm4

    vm5

    That should do the trick for you!

    $vms | Shutdown-VMGuest -Confirm:$false


  • 3.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 12:42 AM

    Something like this should work

    Get-VM | Where {$_.name -match "^VM[1-5]$"} | Shutdown-VMGuest -Confirm:$false

    Of course you can modify the regular expression to do what ever filtering you want.  Of course test before you deploy. :smileywink:

    Edit: Didn't refresh my page, so didn't see there was already a response. Oops. :smileyhappy:

    Message was edited by: AureusStone



  • 4.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 01:12 AM

    Hello, tdubb123-

    Both of the examples above should work out ok.  If you are wanting to keep it a bit simpler, you can use a built-in PowerShell "wildcard pattern".  This allows you to specify a set of characters or a range of characters that you want to be used in your pattern.  Something like:

    Get-VM -Name vm[1-5] | Shutdown-VMGuest -Confirm:$false

    As further illustration, you could use a wildcard pattern "myVM[a-d]0" to get VMs named myVMa0, myVMb0, myVMc0, myVMd0.  For a bit more info, Get-Help about_Wildcards.  Enjoy.



  • 5.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 01:21 AM

    oh snap!  I hadn't looked at get-help about_wildcards before.  I feel like a dunce sometimes!  Nice!

    In truth I generally prefer to generate a list, especially when making changes to systems or the environment.  I find it worthwhile to be able to check the list prior to going full on, though of course that is why -whatif is supposed to be used. :smileyhappy:



  • 6.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 02:49 AM

    Yes, as the old adage goes, "learn something new everyday".  I learned about this particular feature of PowerShell from an example by, of course, LucD.

    Knowing upon what items you are about to act is definitely valuable, and lists are indeed one way to be sure.  I just wanted to bring up another way to go on this one...



  • 7.  RE: get-vm -name vm1-5

    Posted Feb 15, 2012 02:54 AM

    I'm definitely glad you did.



  • 8.  RE: get-vm -name vm1-5

    Posted Nov 19, 2014 09:07 PM

    hi

    I am trying to do get vm for

    vm[12-24]

    but it will not work

    any idea?

    tried using $range too but I only get an output for vm24



  • 9.  RE: get-vm -name vm1-5

    Posted Nov 20, 2014 02:10 AM

    Hello, tdubb123-

    The wildcarding that you are using there will get VMs named "vm1", "vm2", and "vm4".  The square brackets there translate to "one of these characters", where "these characters" are 1, 2, and 4.  To get VMs name vm12, vm13, ..., vm23, vm24, you could use something like:

    Get-VM (12..24 | %{"vm$_"})

    How does that do for you?



  • 10.  RE: get-vm -name vm1-5

    Posted Nov 20, 2014 05:37 AM

    hi

    Yes that works perfectly. however how comes this works as well now? it gives same output

    get-vm vm[1-2][0-9]



  • 11.  RE: get-vm -name vm1-5
    Best Answer

    Posted Nov 20, 2014 01:54 PM

    Hello-

    Your most recent example should work in that it will return VM's 12-24, but, it will not include _only_ 12-24. This example will match 1 or 2 on the first character after "vm", and zero through nine for the second character.  So, putting that together, it would match vm10, vm11, vm12..., vm19, vm20, vm21..., vm27, vm28, and vm29 -- so, instead of vm12 through vm24, it matches vm10 through vm29.

    Now, if you do not have any VMs with numbered names outside of 12 through 24, that would be why your example returned the same.  But, for the day that someone creates vm11, and you do not really want to act on that with this [whatever is the purpose] code, you will want to tighten down those patterns.  See how that is working?



  • 12.  RE: get-vm -name vm1-5

    Posted Nov 20, 2014 09:21 PM

    yes it makes sense now. Thanks a lot