none
Create incremented variables from objects in another variable. RRS feed

  • Question

  • I have a variable $running_vms that has all of the VM names on that host.

    $running_vms = Get-VM | Where-Object State -ne Off | ForEach-Object Name

    I want to create an incremented variable with each VM name in it so that I can run Measure-VM against it.

    I know that i will need a ForEach-Object loop and a way to increment the variable number, and I'm not sure how to get the names in their own variable. 

    $running_vms | ForEach-Object `
    {
        $n++
        New-Variable -Name $vm + $n
    }

    If you could point me to the correct documentation, or provide any assistance I would greatly appreciate it!

    Monday, October 7, 2019 7:00 PM

Answers

  • The Script Gallery did not have quite what I was looking for.

    I ended up with;

    Get-VM | Where State -ne Off | Get-VMHardDiskDrive | Get-VHD


    • Marked as answer by Keystroke-Me Tuesday, October 8, 2019 6:03 PM
    Tuesday, October 8, 2019 6:02 PM

All replies

  • Why?

    Just do this:

    (Get-VM | Where-Object State -ne Off).Count


    \_(ツ)_/

    Monday, October 7, 2019 7:39 PM
  • I am trying to get metrics for the guest OSes, so that I can use something like 

    $VHD_Path = Get-VMHardDiskDrive $vm1 | ForEach-Object {$_.Path}

    Get-VHD $VHD_Path

    Monday, October 7, 2019 8:01 PM
  • What metrics? There are no metrics anywhere in your code.

    You need t ask a clear technical question about a script you have written.  We cannot guess at what you are asking.


    \_(ツ)_/

    Monday, October 7, 2019 8:19 PM
  • I am trying to write a script that will run on a Hyper-V host. This script will query the host for the VMs that are running on said host. Once the script has a list of VMs it can then query the individual VMs for information, such as VHD size, or CPU utilization.

    I do not have a script written yet, I am stuck on getting each of the VM names in their own variable.

    I am writing this script so that I can use it on several different Hyper-V hosts.


    • Edited by Keystroke-Me Monday, October 7, 2019 8:48 PM Edit for clarity
    Monday, October 7, 2019 8:44 PM
  • Why do you think you need all VMs in the a variable.  THe following line places all VMSW in a variuable.

    $vhds = Get-VHD

    Get-VHD | Get-VMHardDiskDrive

    Before posting in a technical forum you should take the time to learn the technology.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Monday, October 7, 2019 8:49 PM
  • Get-VHD requires a VM Name to passed to it. It does not output all of the VHDs on the Hyper-V host.

    I am learning the technology, however I got stuck and came here to request help. If everyone were to "learn the technology" before posting wouldn't that make forums like this pointless?

    I don't know for a fact if they need to be in one variable. My thought is;

    Get VM->Get VHD->Query-VHD

    Get-VM will give me the name of all of the VMs that are running

    Then using Get-VHD I can retrieve the location of the VHD for that VM

    Then using Query-VHD I can get the Size and File Size of the disk and calculate percentage used.

    Monday, October 7, 2019 9:13 PM
  • Get-VM does not require a name. It will get all VMs and then get all associated VHD drives,

    I really recommend learning about how to code in PowerShell,  You are trying to do things without much basic knowledge of PowerShell.

    Try this to see how it works:

    Get-VM | Get-Vhd

    Get-VM | Get-VmHardDiskDrive

    Learn to use help:

    help help

    Read the full help for all CmdLets and be sure to try the examples until you understand how the command works.


    \_(ツ)_/

    Monday, October 7, 2019 9:21 PM
  • Why do you think you need all VMs in the a variable.  THe following line places all VMSW in a variuable.
    
    $vhds = Get-VHD
    
    Get-VHD | Get-VMHardDiskDrive
    
    Before posting in a technical forum you should take the time to learn the technology.

    I agree that Get-VM does not require a VM name, I said Get-VHD. In your example you also used Get-VHD.

    I'm not sure where you are gathering that I don't have much knowledge of powershell, but I appreciate your assitance!

    Monday, October 7, 2019 9:31 PM
  • Please learn PowerShell. This forum is for scripts that you have written and not a pl\ace to ask others to guess atwhat you want. Look in the script Gallery for many scripts that gather statistics from VMs. Someone has already written the script you are looking for.


    \_(ツ)_/

    Monday, October 7, 2019 9:34 PM
  • The Script Gallery did not have quite what I was looking for.

    I ended up with;

    Get-VM | Where State -ne Off | Get-VMHardDiskDrive | Get-VHD


    • Marked as answer by Keystroke-Me Tuesday, October 8, 2019 6:03 PM
    Tuesday, October 8, 2019 6:02 PM