PowerCLI

 View Only
  • 1.  VM to network Map information in excel

    Posted Aug 31, 2009 03:08 AM

    Hi,

    Is it possible to get the report of VM to network Map information in a spreadsheet format using VI toolkit? (similar to the one in VC server, but in Vc server we can get only map diagram)

    Similarly, can we also get the VM to datastore info in spreadsheet?

    If possible, what commad/script has to be used after connecting to VC server through VI toolkit?

    Regards,

    Nagu



  • 2.  RE: VM to network Map information in excel

    Posted Aug 31, 2009 04:54 AM

    Sure. This should do the trick (if your VMs only have 1 NIC)

    Get-VM | select Name, @{L="Network"; E={($_ | Get-NetworkAdapter).NetworkName}}
    

    If your VMs have more than 1 NIC the script gets slightly more complex.

    Get-VM | %{$vm=$_; $_ | Get-NetworkAdapter | select @{L="VMname"; E={$vm.Name}},@{L="Network"; E={$_.NetworkName}}}
    

    Similar process for the datastore mapping.

    Only 1 datastore per VM.

    Get-VM | select Name, @{L="Datastore"; E={($_ | Get-Datastore).Name}}
    

    Multiple datastores per VM.

    Get-VM | %{$vm=$_; $_ | Get-Datastore | select @{L="VMname"; E={$vm.Name}},@{L="DSname"; E={$_.Name}}}
    



  • 3.  RE: VM to network Map information in excel

    Posted Aug 31, 2009 05:01 AM

    Just noticed in the title of the thread that you wanted the info in Excel.

    No problem, just "pipe" the results to the Export-Csv cmdlet.

    For example

    Get-VM | %{$vm=$_; $_ | Get-Datastore | select @{L="VMname"; E={$vm.Name}},@{L="DSname"; E={$_.Name}}} | Export-Csv "C:\VM-DS-map.csv" -NoTypeInformation
    



  • 4.  RE: VM to network Map information in excel

    Posted Aug 31, 2009 05:33 AM

    Hi,

    I'm getting below error messages when I run both the scripts. (I'm runnig this script after connecting to VC server)

    ++ Get-VM | %{$vm=$_; $_ | Get-Datastore | select <<<< @{L="VMname"; E={$vm.Nam+

    e}},@{L="DSname"; E={$_.Name}}} | Export-Csv "C:\temp\VM-DS-map.csv" -NoTypeInf

    ormation

    Select-Object : Illegal key L

    At line:1 char:47

    ++ Get-VM | %{$vm=$_; $_ | Get-Datastore | select <<<< @{L="VMname"; E={$vm.Nam+

    e}},@{L="DSname"; E={$_.Name}}} | Export-Csv "C:\temp\VM-DS-map.csv" -NoTypeInf

    ormation

    Select-Object : Illegal key L

    At line:1 char:47

    ++ Get-VM | %{$vm=$_; $_ | Get-Datastore | select <<<< @{L="VMname"; E={$vm.Nam+

    e}},@{L="DSname"; E={$_.Name}}} | Export-Csv "C:\temp\VM-DS-map.csv" -NoTypeInf

    ormation

    Select-Object : Illegal key L



  • 5.  RE: VM to network Map information in excel

    Posted Aug 31, 2009 06:10 AM

    Try "N" (which stands for Name ) instead of "L" (which stands for Label ).

    I tested the script in PowerShell v2 on my Windows7 box.



  • 6.  RE: VM to network Map information in excel

    Posted Aug 31, 2009 08:23 AM

    Hi,

    Thanks this worked without any error. Bur I was expecting report in a map format. i.e., datastore name->VMs hosted on it instead of present VM>datastore info (Though i got this now after filltering in excel).. is it possible or am I asking too much?

    Regards,

    Nagu



  • 7.  RE: VM to network Map information in excel

    Posted Aug 01, 2014 04:21 PM

    Hi Luc :smileyhappy:

    Here is a PowerCli function I created based on one of your previous posts (thanks) in the community along other peoples posts.

    Being new to PowerCli and Powershell, it was a lot of work but also a great learning experience. Hopefully it will be useful to others in the community.

    The function does the following:

    Takes input piped in from a Get-VM command (1 or multiple VM objects)  and maps the path that given VM takes from it's virtual ethernet adapter(s)

    and maps the following network path information:

    VM Name, VM Virtual Nic, Portgroup, PortGroup VLAN, vSwitch, ESXi Physical NIC(s) attached to vSwitch, ESXi Host, Physical Switch Port(s) via CDP, Physical Switch Port Native VLAN via CDP,Physical Switch Name via CDP, & Observed IP traffic on each ESXi Physical Nic.

    Still todo : Build-in some help functionality and error handling.

    Command usage example:

    Get-VM <VMName> | Get-VMNetPath

    Because the Output of the function are Object(s) it can be piped into other formats, scripts or functions.

    Example export to csv :

    get-vm | Get-VMNetPath | Select VM,VM_Eth,PGVLAN,PortGroup,vSwitch,Host,pNic,pNic_Subs,Switch_NVLAN,Switch_Port,Switch,Switch_IP | export-csv c:\VM_Network_Path.csv -NoTypeInformation

    ## Begin Code ##

    Function Get-VMNetPath {

        #Ref: Luc D Posting,

        #Configured as Function,Added Parameters,Pipeline,Output as Object, & Optimization

        #Added VM vitural Nic , Added CDP info (Physical Switch info)

        [CmdletBinding()]

        param(

              [Parameter(Mandatory=$False,

              ValueFromPipeline=$True,

              ValueFromPipelineByPropertyName=$True,

              HelpMessage="Output from Get-VM")]

              [Alias('VM')]

       [Object[]]$VMs

        )

        BEGIN

        {

            #Create Empty Collection for results of Get-View ESX

            $ESX_Views = @()

        }

       

       

        PROCESS

        {

          

            ForEach($VM in $VMs)

            {

               

                #Define VM Vars

                [String]$VM_Name =''

                [String]$VM_pNic =''

                [String]$VM_vSwitch =''

                [String]$VM_PortGroup =''

                [String]$VM_Host =''

                [String]$VM_VLAN =''

                [String]$VM_Eth =''

                $VM | `

                %{Get-View $_.ID} | `

                    %{

                         $VM_Name = $_.Name

                         $ESX_Name = $_.Runtime.Host.Value

                        

                         #Colletion of VM Virtual Ethernet Cards inc. VM.Name,VM_Eth,PortGroup of VM_Eth

                         $VM_Eths = @()

                         $VM_Eths = ($_.config.hardware.device | where {$_ -is [VMware.Vim.VirtualEthernetCard]}).deviceInfo | Select @{n='VM';exp={$VM.Name} },@{n="VM_Eth";exp={$_.Label}},@{n="PortGroup";exp={$_.Summary}}

                         # Check if Get-View of Host is Cached in (Collection) $ESX_Views

                         # If Cached, use Cached, if not Get-View of Host and add to $ESX_Views Collection

                         if  ( ($ESX_Views | Where {$_.Config.Host.Value -eq $ESX_Name}) -eq $null )

                         {

                            #Add View to $ESX_Views Collection

                            $ESX_Views += Get-View -ID $_.Runtime.Host

                         }

                        

                         $esx = $ESX_Views | Where {$_.Config.Host.Value -eq $ESX_Name}

                         #Collect Physical Network CDP info from ESXi Host

                         $esx_Net_View = Get-View $esx.ConfigManager.NetworkSystem

                         $esx_CDP = @()

                        

                        foreach($PhysNic in $esx_Net_View.NetworkInfo.Pnic)

                        {

                            $PnicInfo = $esx_Net_View.QueryNetworkHint($physnic.Device)

                            foreach($Hint in $PnicInfo)

                            {

                                $ObservIPs = @()

                                foreach($subnet in $hint.subnet)

                                {

                               

                                    $ObservIPs += $subnet.IPsubnet

                               

                                }

                                if( ($hint.ConnectedSwitchPort)  )

                                 {

                                    #Put CDP info into a Collection for matching against pNIC

                                    #$hint.Subnet #test

                                   

                                    $esx_CDP += ('' | Select @{n='HostName'; exp={$Esx.Name} }, @{n='pNic';exp = {$PhysNic.Device} } , `

                                    @{n='Switch';exp={$Hint.ConnectedSwitchPort.Devid} } , @{n='Switch_IP' ;exp={$Hint.ConnectedSwitchPort.Address} } , `

                                    @{n='Switch_Port';exp={$Hint.ConnectedSwitchPort.PortId} } , @{n='Switch_NVLAN';exp= {$Hint.ConnectedSwitchPort.Vlan} } , `

                                    @{n='ObservIPs'; exp={$ObservIPs}} )   

                                 }

                                

                                 Else

                                

                                 {

                                    $esx_CDP += ('' | Select @{n='HostName'; exp={$Esx.Name} }, @{n='pNic';exp = {$PhysNic.Device} } , `

                                    @{n='Switch';exp={"N/A"} }, @{n='Switch_IP' ;exp={"N/A"} } , `

                                    @{n='Switch_Port';exp={"N/A"} } , @{n='Switch_NVLAN';exp= {"N/A"} } , `

                                    @{n='ObservIPs'; exp={$ObservIPs}} )   

                                

                                

                                 }

                            }

                           

                        }

                        foreach($nicImpl in $_.Network)

                        {

                            $nic = Get-View $nicImpl

                            $VM_PortGroup=$nic.Name

                            $VM_Host = $esx.Name

               

                            foreach($hnet in $esx.Config.Network.Portgroup)

                            {

                              if($hnet.Spec.Name -eq $nic.Name)

                              {

                                $VM_VLAN = $hnet.Spec.VlanId

                                $VM_vSwitch = $hnet.Spec.VswitchName

                                if($hnet.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -is [array])

                                {

                                  foreach($pnic in $hnet.ComputedPolicy.NicTeaming.NicOrder.ActiveNic)

                                  {

                                    #Define additional Vars for Object.

                                    $VM_pNic = $pnic

                                    $VM_Eth = ($VM_Eths | Where {$_.PortGroup -eq $VM_PortGroup}).VM_Eth

                                    $Switch = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch

                                    $Switch_IP = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_IP

                                    $Switch_Port = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_Port

                                    $Switch_NVLAN = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_NVLAN

                                    $ObservIPs = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).ObservIPs

                                    $props = @{'Host'= $VM_Host;

                                               'pNic'= $VM_pNic;

                                               'vSwitch' = $VM_vSwitch;

                                               'PortGroup' = $VM_PortGroup;

                                               'VM' = $VM_Name;

                                               'PGVLAN' = $VM_VLAN;

                                               'VM_Eth' = $VM_Eth;

                                               'Switch' = $Switch;

                                               'Switch_IP' = $Switch_IP;

                                               'Switch_Port' = $Switch_Port;

                                               'Switch_NVLAN'= $Switch_NVLAN;

                                               'pNic_Subs'= $ObservIPs}

                                    $obj = New-Object -TypeName PSObject -Property $props

                                    Write-Output $obj

                                  }

                                }

                              }

                            }

                        }

                      }

       

            }

        }

       

       

        END{}

        

    }

    ## End Code ##



  • 8.  RE: VM to network Map information in excel

    Posted Aug 01, 2014 04:45 PM

    p.s.

    The explanation was intended for others who may be reading this post. I know that you really know your stuff and I have a tremendous amount of respect for what you have contributed to the community.

    Thanks,

    Frank :-)



  • 9.  RE: VM to network Map information in excel

    Posted Aug 01, 2014 05:02 PM

    Great script, thanks for sharing