10 Replies Latest reply on May 24, 2019 1:20 AM by kakekmuda

    How to filter vm by datacenter name

    kakekmuda Novice

      [ASK]

       

      How to grab vm by datacenter name and grab status vm on/off  ?

      i just want to grab KP2 Virtual Data Center/ another data center

      this is my code please help, i was try to change/add code but its still error..

      &{foreach($vm in (Get-VM)) {

       

       

          $vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name.split('(')[0].TrimEnd(' ')}},

         @{N='PIC Aplikasi';E={$vm.Name.split('()')[1]}},

          @{N='OS';E={$vm.Guest.OSFullName}},

      @{N="NumCPU";E={$vm.NumCPU}},

      @{N="MemoryMB";E={$vm.MemoryMB}},

          @{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},

          @{N='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

          @{N='Subnet Mask';E={

                      $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

                      $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {

                              $Remainder = $dec % [Math]::Pow(256, $i)

                              (                        $dec - $Remainder) / [Math]::Pow(256, $i)

                              $dec = $Remainder

                          } )

                      [String]::Join('.', $DottedIP)

                  }},

              @{N='MAC';E={[string]::Join(',',$_.MacAddress)}}

        }

      } | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv

       

       

       

      Thank You So Much

      Tomi Irawan

        • 1. Re: How to filter vm by datacenter name
          LucD Guru
          Community WarriorsvExpertUser Moderators

          You mean like this?

           

          $dcName = 'KP2 Virtual Data Center'

          & { foreach ($vm in (Get-Datacenter -Name $dcName | Get-VM))

             {

             $vm.ExtensionData.Guest.Net | select -Property @{N = 'VM'; E = { $vm.Name.split('(')[0].TrimEnd(' ') } },

             @{N = 'State'; E = { $vm.PowerState } },

             @{N = 'PIC Aplikasi'; E = { $vm.Name.split('()')[1] } },

             @{N = 'OS'; E = { $vm.Guest.OSFullName } },

             @{N = "NumCPU"; E = { $vm.NumCPU } },

             @{N = "MemoryMB"; E = { $vm.MemoryMB } },

             @{N = 'IP'; E = { [string]::Join(',', ($vm.Guest.IPAddress | Where { ($_.Split(".")).length -eq 4 })) } },

             @{N = 'Gateway'; E = { [string]::Join(',', ($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | % { if ($_.Gateway.IpAddress) { $_.Gateway.IpAddress } })) } },

             @{N = 'Subnet Mask'; E = {

             $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

             $DottedIP = $( For ($i = 3; $i -gt -1; $i--)

             {

             $Remainder = $dec % [Math]::Pow(256, $i)

             (   $dec - $Remainder) / [Math]::Pow(256, $i)

             $dec = $Remainder

             } )

             [String]::Join('.', $DottedIP)

             }

             },

             @{N = 'MAC'; E = { [string]::Join(',', $_.MacAddress) } }

             }

          } | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv

          • 2. Re: How to filter vm by datacenter name
            kakekmuda Novice

            Dear LuCD

             

            Thank You for your fast respond. i wil try it first thank you so much

            • 3. Re: How to filter vm by datacenter name
              kakekmuda Novice

              Dear LucD

               

              Please help me how to split this table,

              sample i have vm who have a lot of ip, ip1,ip2 and gateway 1,gateway2.etc , can i split this ?

               

              thank you so much

              • 4. Re: How to filter vm by datacenter name
                LucD Guru
                Community WarriorsvExpertUser Moderators

                The question you will have to answer first is, how do you want to show this in your output?
                If an array has rows with different numbers of properties, there is a problem with for example Export-Csv.
                To guarantee the same number of properties/columns, you could for example create columns like IP1, IP2, IP3, IP4.
                But that would mean that several rows have no values in some columns.
                You decide.

                • 5. Re: How to filter vm by datacenter name
                  kakekmuda Novice

                  Dear LucD

                   

                  Thank you for your fast resond .

                   

                  look like this is no problem because there is some vm who have a more IP & Gateway and some server have the only one IP & Gateway.

                  Header 1Header 2Header 3Header 4
                  vm AIP1IP2
                  vm BIP1
                  vm CIP1IP2

                  IP3

                   

                  Thank You For your help.

                  • 6. Re: How to filter vm by datacenter name
                    LucD Guru
                    Community WarriorsvExpertUser Moderators

                    Not sure what you are trying to say here.
                    But the point I'm trying to make is demonstrated by this example

                     

                    $t1 = @(

                       "" | Select @{N='F1';E={'abc'}}

                       "" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}

                    )

                    $t2 = @(

                       "" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}

                       "" | Select @{N='F1';E={'abc'}}

                    )


                    $t1 | Export-Csv -Path .\report1.csv -NoTypeInformation -UseCulture

                    Invoke-Item -Path .\report1.csv

                    $t2 | Export-Csv -Path .\report2.csv -NoTypeInformation -UseCulture

                    Invoke-Item -Path .\report2.csv

                    • 7. Re: How to filter vm by datacenter name
                      kakekmuda Novice

                      Dear LucD

                      thank you for the answer but, i want the text between in a separate column in one report.?

                       

                      Thank You Mr. LucD

                      • 9. Re: How to filter vm by datacenter name
                        LucD Guru
                        vExpertCommunity WarriorsUser Moderators

                        You didn't really answer my question.
                        For Export-Csv all the rows should have the same number of properties.
                        How do you intend to display that?
                        Would you go for a fixed number of (potentially empty) columns for IP, Gateway and netmask?

                        • 10. Re: How to filter vm by datacenter name
                          kakekmuda Novice

                          Dear Mr. LucD

                          Sorry for that,

                          it doesn't matter for a few empty columns because it doesn't mean the number of ip and gateway in one vm.

                          can separate columns according to the number of texts ?

                           

                          Thank You So Much