locked
No VmName in output RRS feed

  • Question

  • My powershell script will not display vm name in output csv. What am I missing?

    $allvms = @()
    $vms = Get-Content C:\Users\XXXXX\Documents\Linux\ALPtest.csv

    foreach($vm in $vms)
    {
      $vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
      $vmstat.VmName = $vm.name
      
      $statcpu = Get-Stat -Entity ($vm)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average
      $statmem = Get-Stat -Entity ($vm)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average

      $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
      $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
      
      $vmstat.CPUMax = $cpu.Maximum
      $vmstat.CPUAvg = $cpu.Average
      $vmstat.CPUMin = $cpu.Minimum
      $vmstat.MemMax = $mem.Maximum
      $vmstat.MemAvg = $mem.Average
      $vmstat.MemMin = $mem.Minimum
      $allvms += $vmstat
    }
    $allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "C:\Perf\Alp-VMstest.csv" -noTypeInformation

    Monday, February 11, 2019 9:21 PM

All replies

  • You are setting the $vmname before you have retrieved it.


    \_(ツ)_/

    • Proposed as answer by Ardian Putra Monday, February 11, 2019 10:51 PM
    Monday, February 11, 2019 9:46 PM
  • If you would actually take the time to actually learn PowerShell all of this would be easy. You are just copying old and obsolete code and trying to use it without understanding how it works.

    Get-Content C:\Users\XXXXX\Documents\Linux\ALPtest.csv |
        ForEach-Object{
            $vm = $_
            $statcpu = Get-Stat -Entity ($vm) -start (get-date).AddDays(-30) -Finish (Get-Date) -MaxSamples 10000 -stat cpu.usage.average
            $statmem = Get-Stat -Entity ($vm) -start (get-date).AddDays(-30) -Finish (Get-Date) -MaxSamples 10000 -stat mem.usage.average
            
            $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
            $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
            
            [pscustomobject]@{
                VmName = $vm
                CPUMax = $cpu.Maximum
                CPUAvg = $cpu.Average
                CPUMin = $cpu.Minimum
                MemMax = $mem.Maximum
                MemAvg = $mem.Average
                MemMin = $mem.Minimum
            }
        } | 
            Select-Object VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | 
            Export-Csv C:\Perf\Alp-VMstest.csv 

    If your file is not a CSV then this will work.  If it is truly a CSV then you would need to use "Import-Csv" to read the file.

    Start by reading the help for all CmdLets you are trying to use until you understand how they work.


    \_(ツ)_/

    Monday, February 11, 2019 9:52 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, February 28, 2019 2:34 PM