Trying to get a VM count on a host, why doesn’t this work?

Welcome Forums General PowerShell Q&A Trying to get a VM count on a host, why doesn’t this work?

Viewing 12 reply threads
  • Author
    Posts
    • #243701
      Participant
      Topics: 6
      Replies: 8
      Points: 82
      Rank: Member

      See below:

      [code]

      $vhost = “10.16.14.25”

      function get-vmcount
      {
      $script:myvmcount = get-vmhost $vhost | Select @{N=“NumVM“;E={($_ | Get-VM).Count}}
      #write-host “Total VM’s on host $vhost = $myvmcount”
      $myvmcount

      }

      get-vmcount
      write-host “VM= $myvmcount”

      [/code]

      all I get in the output is:

      VM= @{NumVM=3}

       

      Why?

    • #243710
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      It’s exactly what you’re asking for. What output are you expecting to get?

    • #243719
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Not sure what benefit you are getting wrapping it in a function, but to answer your question you are returning an object and referencing the entire object versus a property in the object:

    • #243728
      Participant
      Topics: 6
      Replies: 8
      Points: 82
      Rank: Member

      Only had a function so I can reuse in other parts of a huge script. And I understand your explanation, had an idea it was something like that.

      Just to be able to print out “The total number of VM’s on host HOST” = 3 (which is the actual number of VM’s on the host). I can do that.

      Thank you.

    • #243833
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      This way it is even simpler…

      Thank you.

       

    • #243842
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      If you’re looking for simpler I’d recommend this snippet:

      [/crayon]

      😉

    • #243905
      Participant
      Topics: 6
      Replies: 8
      Points: 82
      Rank: Member

      If you’re looking for simpler I’d recommend this snippet:

      $vhost = '10.16.14.25'<br />[pscustomobject]@{<br />    VMCount = (Get-VM -ComputerName $vhost).Count<br />} 😉

      Olaf- I get the error “a parameter cannot be found that matches parameter name ‘ComputerName’. Verified code I entered to yours.

    • #243911
      Participant
      Topics: 6
      Replies: 8
      Points: 82
      Rank: Member

      If you’re looking for simpler I’d recommend this snippet:

      $vhost = '10.16.14.25'</p><p>[pscustomobject]@{</p><p>VMCount = (Get-VM -ComputerName $vhost).Count</p><p>} 😉

      Olaf- I get the error “a parameter cannot be found that matches parameter name ‘ComputerName’. Verified code I entered to yours.

      Also I am using VMware for virtualization

    • #243935
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      Okay, if it isn’t Hyper-V then what Rob suggested is the ideal way, but still, it can be in this way as well…

    • #243962
      Participant
      Topics: 6
      Replies: 8
      Points: 82
      Rank: Member

      Okay, if it isn’t Hyper-V then what Rob suggested is the ideal way, but still, it can be in this way as well…

      $vhost = “10.16.14.25” [pscustomobject]@{ VMCount = (Get-VMHost -Name $vhost | Get-VM).Count } Thank you.

      Well, that works, sorta. I would like to print a line like “Toatal VM’s =” xx. Which is the variable I use, VMCount? Anyway after I incorporated this line in my code the command does not output anything until I exit the script. Maybe out-host? I had this issue before but damned if I remember how I fixed it.

       

      …Alan

    • #243965
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Like this?

    • #244088
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      What is the point of wrapping this into an object? It can simply be a variable:

    • #244121
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Agreed if it’s just to write to the screen.

Viewing 12 reply threads
  • You must be logged in to reply to this topic.