6 Replies Latest reply on Jan 27, 2012 12:54 AM by Arjantimvdi201110141

    Need a script to get hosts where observed IP-Range

    Arjantimvdi201110141 Enthusiast

      For a customer I need to filter which Hosts (and VM's) have a connection with the DMZ VLAN's.

       

      Problem is, the physical and virtual network VLAN's over multiple vCenter's aren't cosistent. So it can occur physical VLAN 24 is presented in the virtual network as VLAN 24 in one cluster and VLAN356 in an other.

       

      My idea is to create a script that will search for observed IP-ranges (like 192.168.128.xxx) and show the VLAN Name, VLANID and VMHost information.

       

      I'd like to get a csv file with this information, to be able to sort the outcome.

       

      I really don't see if/how this can be done, and so I ask the help of the GURU's

       

      Thanks in advance!

       

      Arjan

        • 1. Re: Need a script to get hosts where observed IP-Range
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Could this help.

          It will run through all portgroups, determine the VMs that have a connection over that portgroup and list the IP address(es) used by that VM over that portgroup.

           

          foreach($esx in Get-VMHost){
              $vms = Get-VM -Location $esx
             
          foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}}     } }
          1 person found this helpful
          • 2. Re: Need a script to get hosts where observed IP-Range
            AlbertWT Master

            Thanks for sharing the script,

             

            how to list the name of the VM here ?

             

            I tried to do the $vms. but the properties & Method doesn't give me anything for displaying VM name at all ?

            • 3. Re: Need a script to get hosts where observed IP-Range
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              You can add one more calculated property on the Select-Object cmdlet.

              Something like this

               

              foreach($esx in Get-VMHost){
                  $vms = Get-VM -Location $esx
                  foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}},             @{N="VM";E={$_.Name}}     } }
              1 person found this helpful
              • 4. Re: Need a script to get hosts where observed IP-Range
                Arjantimvdi201110141 Enthusiast

                I really can't get it exported to a csv file :-( tried Export-Csv on multiple locations, but it doesn't seem to work.

                 

                Please help? ;-)

                 

                Greets, and a million thanks for every thing done already.

                • 5. Re: Need a script to get hosts where observed IP-Range
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  Did you try it like this ?

                   

                  $report = foreach($esx in Get-VMHost){
                      $vms = Get-VM -Location $esx
                      foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}},             @{N="VM";E={$_.Name}}     } } $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
                  • 6. Re: Need a script to get hosts where observed IP-Range
                    Arjantimvdi201110141 Enthusiast

                    Thanks Luc,

                     

                    The Dutch government is very grateful to you This really makes their live much easier.

                     

                    Have a great weekend!