none
Powershell - loop question RRS feed

  • Question

  • I have a script which is visible in the screenshot and I don't know why the values in $vmlist cannot be accessed.
    I'm creating the list, then foreach"ing" them, but running a command against them is erroring out on each loop.
    HyperVisor cannot find machine name. I can assure you the machine names are correct.

    I think it's a result of the Type of list created for $vmList (ie.. not true text...??)

    $vmlist=Get-VM | Where State -eq Running | Select Name
    if ($vmlist) {
      foreach ($vmname in $vmlist) {
            Stop-VM -name $vmname 
    }}


    Can anyone help?


    • Edited by bflagg Thursday, December 12, 2019 12:27 PM For some reason on post 3, the simple text cannot be viewed. Changed to 'code' view'
    Wednesday, December 11, 2019 5:12 AM

Answers

  • Again why? Why all of the code when we have PowerShell. This is not Basic or VBScript:

    Get-VM | Where State -eq Running | Stop-VM


    \_(ツ)_/


    • Edited by jrv Saturday, December 14, 2019 3:41 AM
    • Proposed as answer by BOfH-666 Saturday, December 14, 2019 11:07 AM
    • Marked as answer by Richard MuellerMVP, Moderator Saturday, December 21, 2019 1:05 PM
    Saturday, December 14, 2019 3:41 AM

All replies

  • An Image of your code is not helpful !!! And you even managed to hide the actual piece of code. 

    Please edit your question and post the code correctly: How to Use the Code Feature in a TechNet Forum Post

    If you get error messages you should post them completely and format them as code as well.

    Thanks.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, December 11, 2019 5:55 AM
  • I didn't know about the image thing...
    code:
    $vmlist=Get-VM | Where State -eq Running | Select Name
    if ($vmlist) {
      foreach ($vmname in $vmlist) {
            Stop-VM -name $vmname
    }}

    $vmlist returns a list of VM names that are running
    (from the pop up gif I captured so you can see the variables values)

    Values in Variable: (as in the screen capture)
    $vmlist = Name
    ------------
    DC01
    DominoServer
    FS01
    VPC-Apps

    When looping with the foreach I get:
    Stop-VM : Hyper-V was unable to find a virtual machine with the name "@{Name=DC01}"
    At c:\Batch\TestVM.ps1:6 char:3
    Stop-vm -Name $vmname
    ObjectNotFound.

    Sorry for the trouble.

    Thursday, December 12, 2019 2:10 AM
  • Please read all links posted to help you learn how to create a technical post and edit you original post to fix the issues.

    How to Use the Code Feature in a TechNet Forum Post

    Also read the following helpful articles that5 will allow you to be understood and get you an answer more quickly.


    \_(ツ)_/


    • Edited by jrv Thursday, December 12, 2019 5:40 AM
    Thursday, December 12, 2019 5:39 AM
  • I didn't know about the image thing...

    That's why we try to teach it to you. But obviously you refuse to read and to learn. I asked you to EDIT your post post the code correctly.

    $vmlist returns a list of VM names that are running

    That's just half of the truth. It returns an object with a property named "Name" of the running VMs.

    Stop-VM : Hyper-V was unable to find a virtual machine with the name "@{Name=DC01}"

    "@{Name=DC01}" is the representation of the object with the property "Name". So you will have to use only the property of the object. Try to learn how to call a single property of a given object and you will know what's wrong in your code snippet

    ... and please read the help we linked for you.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Thursday, December 12, 2019 1:13 PM
    Thursday, December 12, 2019 7:07 AM
  • Here is a free book that will help you to learn PowerShell. Guessing will waste 100+ times as much time as it takes to do the tutorial in the book.

    Windows-PowerShell TFM Fourth Edition

    It is currently the best beginner training I can find that is free.


    \_(ツ)_/

    Thursday, December 12, 2019 7:13 AM
  • Ok. so your kinda mean.

    I work hard all day, got a toddler, work on 3-5 hours a sleep a night and I get old school arrogance RTFM.

    and I have been reading. but it's difficult due to reason above. I was hoping for a little compassion and for someone to help how to capture that value. As I have done it before, but it's been years and things are foggy.
    At least where and what to look up.

    Open your damn heart!

    Off to wake the toddler to get her to day care and me to work.
    Thanks your for "time"

    Thursday, December 12, 2019 12:13 PM
  • Sorry but your personal issue are not relevant here. You still need to learn the technology. Learning it will save you time and allow you more time to sleep.

    Also you original post is unreadable which makes any help nearly impossible.  How can you expect us to read an image that is no larger than a postage stamp?  How can we know what you are asking when your request is hard to understand.

    There is too little information to even suggest an answer.  All of these issues are because you do not have enough technical background to ask a clear question.

    Sorry but we cannot help you unless you can somehow fix these issues.


    \_(ツ)_/

    Thursday, December 12, 2019 12:22 PM
  • figured that would be your reply.

    Have a nice day

    Thursday, December 12, 2019 12:29 PM
  • figured that would be your reply.

    Have a nice day

    If you don't care to fix your post what can we possibly do.


    \_(ツ)_/

    Thursday, December 12, 2019 1:11 PM
  • Try below:

    $vmlist=Get-VM | Where State -eq Running | Select -ExpandProperty Name
    if ($vmlist) {
      foreach ($vmname in $vmlist) {
            Stop-VM -name $vmname 
    }}
    This might be helpful for you : https://devblogs.microsoft.com/powershell/select-expandproperty-propertyname/
    • Edited by DumbleD0re Friday, December 13, 2019 11:55 AM
    Friday, December 13, 2019 11:54 AM
  • Again why? Why all of the code when we have PowerShell. This is not Basic or VBScript:

    Get-VM | Where State -eq Running | Stop-VM


    \_(ツ)_/


    • Edited by jrv Saturday, December 14, 2019 3:41 AM
    • Proposed as answer by BOfH-666 Saturday, December 14, 2019 11:07 AM
    • Marked as answer by Richard MuellerMVP, Moderator Saturday, December 21, 2019 1:05 PM
    Saturday, December 14, 2019 3:41 AM
  • Again why? Why all of the code when we have PowerShell. This is not Basic or VBScript:

    Get-VM | Where State -eq Running | Stop-VM


    \_(ツ)_/


    Was just trying to help bflagg with the use of -ExpandProperty, obviously using pipe is the way someone would be doing the same in powershell.
    Saturday, December 14, 2019 8:58 AM
  • Thank you DumbleD0re...

    I knew about what the consultant said and tried, but I needed to shut down VMs in order. (and start up)

    Using the script allows me to test for machine name first. Straight PS (as far as I could find) doesn't for the finesse of shutting down machines in a sequence..

    For one example..shutting down Domain Controller last, starting it first. File server second to last, File server starting second, wsus, 3rd to last, 3rd to load.. the rest doesn't matter.

    Thank you for your time and help.

    Brett


    • Edited by bflagg Wednesday, February 12, 2020 4:13 AM
    Wednesday, February 12, 2020 4:08 AM