6 Replies Latest reply on Jan 21, 2015 8:34 PM by Sivaramsharmar

    VM OS Count

    Sivaramsharmar Enthusiast

      Hi All,

      I am trying to find VM with OS Details, as we can get Guest OS Details when VMWare tools are running on it.

      So I am trying to find the OS from VM Side.

      When I am executing below command

      (Get-View -ViewType virtualmachine | select -ExpandProperty summary | select -ExpandProperty config | where{$_.guestfullname -like "*win*"} | measure-object).count

      I am getting exact output when I am trying in

      get-vm | select @{N='Windows';E={($_ | get-view | select -ExpandProperty summary | select -ExpandProperty config | where{$_.guestfullname -like "*win*"} | measure-object).count}}

      I am not getting count instead of it is counting each one by one.

      If I got details using above command then I can generate Chart that is my motto.

      Please suggest.

        • 1. Re: VM OS Count
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Try like this

           

          (Get-View -ViewType VirtualMachine -Property Name,'Summary.Config.GuestFullName' `

              -Filter @{'Summary.Config.GuestFullName'='Win'}).Count

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: VM OS Count
            Sivaramsharmar Enthusiast

            Thanks Lucd,

            If I am executing your command individually I am getting output as exact,

            but my output is supposed to be

            Windows        Linux              Others               Total

            169                    45                    8                       222

             

            For this I am trying to execute like below

            Get-VM | Select @{N='Windows';E={($_ | get-view | select -expandproperty summary | select -expandproperty config | where{$_.guestfullname -like "*Win*"}| measure-object).count}}.........

             

            Like this I am trying to execute but I am not getting expected results.

            Please suggest.

            • 3. Re: VM OS Count
              Zsoldier Expert
              vExpert

              Riding on Luc's coat tails here is how I would write something that you are looking for like so:

               

               

               

              $TotalVMs = Get-View -ViewType VirtualMachine -Property Name,'Summary.Config.GuestFullName'

              $OSFilters = $TotalVMs | select -ExpandProperty summary | select -expandproperty config | select -unique guestfullname | sort

               

               

              $MyCustomReport = New-Object PSObject

              Foreach ($OSFilter in $OSFilters)

                   {$MyCustomReport | Add-Member -Name $OSFilter.GuestFullName -MemberType NoteProperty -Value ($totalvms | select -expandproperty summary | select -expandproperty config | where {$_.GuestFullName -eq $OSFilter.guestfullname}).count}

              $MyCustomReport | Add-member -Name Total -MemberType NoteProperty -Value $TotalVMs.Count

               

               

              $MyCustomReport

               

               

              This way you can continue to use this script even if 'other' OS types get added without having to mess w/ static filters.

              1 person found this helpful
              • 4. Re: VM OS Count
                MKguy Virtuoso

                This should work:

                $OSList = (Get-VM).ExtensionData.Config.GuestFullName

                "" | Select @{N='Windows'; E={ ($OSList | Where { $_ -like '*windows*' } | Measure).Count }},

                @{N='Linux'; E={ ($OSList | Where { $_ -like '*linux*' } | Measure).Count }},

                @{N='Other'; E={ ($OSList | Where { $_ -inotmatch '(linux|windows)' } | Measure).Count }} | Format-Table -Autosize

                 

                This gives me output like this:

                Windows  Linux   Other   Total

                -------       -----     -----      -----

                     98         37         3        138

                • 5. Re: VM OS Count
                  Sivaramsharmar Enthusiast

                  Your Script will return OS Flavour as well. Thanks for your support Chris.

                  • 6. Re: VM OS Count
                    Sivaramsharmar Enthusiast

                    Thanks MK,

                    Your Script will return the desired output,

                    But it is not identifying CentOS, So I have did one small correction.

                    $OSList = (Get-VM).ExtensionData.Config.GuestFullName

                    $vmre = "" |select @{N='Windows'; E={ ($OSList | Where { $_ -like '*wind*' } | Measure).Count }},

                    @{N='Linux'; E={ ($OSList | Where { $_ -inotmatch '(other|wind)' } | Measure).Count }},

                    @{N='Other'; E={ ($OSList | Where { $_ -like '*other*' } | Measure).Count }} | Format-Table -Autosize

                     

                     

                    More Over I am trying to generate Pie Chart for this,I have referred this blog http://blogs.technet.com/b/richard_macdonald/archive/2009/04/28/3231887.aspx

                    but I am not able to do so, Could you please help me where I am doing wrong.

                    [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

                    $scriptpath = "c:\users\$env:username\"

                     

                    # chart object

                       $chart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart

                       $chart1.Width = 1200

                       $chart1.Height = 500

                       $chart1.BackColor = [System.Drawing.Color]::White

                     

                    # title

                       [void]$chart1.Titles.Add("Virtual Machine Operating System Report")

                       $chart1.Titles[0].Font = "Arial,13pt"

                       $chart1.Titles[0].Alignment = "topLeft"

                     

                    # chart area

                       $chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea

                       $chartarea.Name = "ChartArea1"

                       $chartarea.AxisY.Title = "count"

                       $chartarea.AxisX.Title = "OS Type"

                       $chartarea.AxisY.Interval = 10

                       $chartarea.AxisX.Interval = 1

                      

                      

                       $chart1.ChartAreas.Add($chartarea)

                     

                    # legend

                       $legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend

                       $legend.name = "Legend1"

                       $chart1.Legends.Add($legend)

                     

                    # data source

                      

                    $datasource = $vmre | select Windows,Linux,Other

                     

                     

                    # data series

                       [void]$chart1.Series.Add("VM Usage")

                       $chart1.Series["VM Usage"].ChartType = "Column"

                       $chart1.Series["VM Usage"].IsVisibleInLegend = $true

                       $chart1.Series["VM Usage"].BorderWidth  = 3

                       $chart1.Series["VM Usage"].chartarea = "ChartArea1"

                       $chart1.Series["VM Usage"].Legend = "Legend1"

                       $chart1.Series["VM Usage"].color = "#62B5CC"

                       $chartArea.AxisX.LabelStyle.Enabled = $true

                       $chartarea.AxisX.LabelStyle.Angle = "-45"

                      $datasource | foreach-object{

                    $chart1.Series["VM Usage"].Points.addxy( $_.Windows,$_.linux,$_.other)

                    $Chart1.Series["VM Usage"]["DrawingStyle"] = "Cylinder"

                    #Remove # Below if you want report in Pie Chart

                    $Chart1.Series["VM Usage"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie

                    $Chart1.Series["VM Usage"]["PieLabelStyle"] = "Outside"

                    $Chart1.Series["VM Usage"]["PieLineColor"] = "Black"

                    $Chart1.Series["VM Usage"]["PieDrawingStyle"] = "Concave"

                    #($Chart1.Series["VM Usage"].Points.FindMaxByValue())["Exploded"] = $true

                    #($Chart1.Series["VM Usage"].Points.FindMinByValue() | select -First 1)["Exploded"] = $true

                    }

                     

                     

                    $maxValuePoint = $Chart1.Series["VM Usage"].Points.FindMaxByValue() 

                    $maxValuePoint.Color = [System.Drawing.Color]::Red

                    $minValuePoint = $Chart1.Series["VM Usage"].Points.FindMinByValue()  

                    $minValuePoint.Color = [System.Drawing.Color]::Green

                     

                     

                    $chart1.SaveImage("$scriptpath\vCenter_VirtualMachine_report.png","png")