PowerCLI

Expand all | Collapse all

How to get vm vlan-id into script ?

  • 1.  How to get vm vlan-id into script ?

    Posted May 09, 2012 11:42 AM

    How can i get the vm's vlan nr (2 nic's) is in the script below?

    i found this line, but


        $vmvlan = Get-VirtualPortgroup -VM $_.Name | %{$_.VlanId}

        $vmvlan[0]
        $vmvlan[1]

    i cant get it to work in my existing script.

    Regards Jeroen.

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

    $ExportFilePath = "d:\temp\Export-VMInfo.csv"
    #
    $Report = @()
    $VMs = Get-VM

    $Datastores = Get-Datastore | select Name, Id
    $VMHosts = Get-VMHost | select Name, Parent
     
    ForEach ($VM in $VMs) {
          $VMView = $VM | Get-View
          $VMInfo = {} | Select VMName,Powerstate,OS,Description,IPAddress,IPAddress2,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,DiskFree,DiskUsed
          $VMInfo.VMName = $vm.name
          $VMInfo.Powerstate = $vm.Powerstate
          $VMInfo.OS = $vm.Guest.OSFullName
          $VMInfo.Description = $vm.Description
          $VMInfo.IPAddress = $vm.Guest.IPAddress[0]
          $VMInfo.IPAddress2 = $vm.Guest.IPAddress[1]
          $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
          $VMInfo.Host = $vm.host.name
          $VMInfo.Cluster = $vm.host.Parent.Name
          $VMInfo.Datastore = ($Datastores | where {$_.ID -match ($vmview.Datastore | Select Value).Value} | Select Name).Name
          $VMInfo.NumCPU = $vm.NumCPU
          $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
          $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
          $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
          $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
          $Report += $VMInfo
    }
    $Report = $Report | Sort-Object VMName
    IF ($Report -ne "") {
    $report | Export-Csv $ExportFilePath -NoTypeInformation
    }



  • 2.  RE: How to get vm vlan-id into script ?

    Posted May 09, 2012 11:47 AM

    Are these dvSwitch portgroups ?

    Have a look at All VM's with VLAN's, that script covers both types



  • 3.  RE: How to get vm vlan-id into script ?

    Posted May 09, 2012 12:00 PM

    Luc we don't use dvSwitch portgroups

    Regards, Jeroen



  • 4.  RE: How to get vm vlan-id into script ?

    Posted May 09, 2012 12:08 PM

    You are passing an array, convert the array to a single string.

    $vm = Get-VM MyVM
    $vmvlan = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))


  • 5.  RE: How to get vm vlan-id into script ?

    Posted Dec 02, 2024 07:57 AM

    Hi LucD,
    I think there may have been mistakes in the original script for cluster and hostname:

          $VMInfo.Host = $vm.host.name
          $VMInfo.Cluster = $vm.host.Parent.Name

    should be:

          $VMInfo.Host = $vm.VMhost.name
          $VMInfo.Cluster = $vm.VMhost.Parent.Name

    For my purposes I want to pull information from a specific set of VMs contained in a csv file, After adding the VLANID I came up with something like this:


    $VMnames = Import-Csv C:\output\sourceVMs.csv
    $ExportFilePath = "c:\output\Import_export_VMs_VLANs.csv"
    $Report = @()
    $Datastores = Get-Datastore | select Name, Id
    $VMHosts = Get-VMHost | select Name, Parent
     
    ForEach ($VMname in $VMnames) {
        $VM = get-VM -name $VMname.name
        $vmvlan = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))
          $VMView = $VM | Get-View
          $VMInfo = {} | Select VMName,Powerstate,OS,Description,IPAddress,IPAddress2,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,DiskFree,DiskUsed,VLAN,Notes
          $VMInfo.VMName = $vm.name
          $VMInfo.Powerstate = $vm.Powerstate
          $VMInfo.OS = $vm.Guest.OSFullName
          $VMInfo.Description = $vm.Description
          $VMInfo.IPAddress = $vm.Guest.IPAddress[0]
          $VMInfo.IPAddress2 = $vm.Guest.IPAddress[1]
          $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
          $VMInfo.Host = $vm.VMhost.name
          $VMInfo.Cluster = $vm.VMhost.Parent.Name
          $VMInfo.Datastore = ($Datastores | where {$_.ID -match ($vmview.Datastore | Select Value).Value} | Select Name).Name
          $VMInfo.NumCPU = $vm.NumCPU
          $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
          $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
          $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
          $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
          $VMinfo.VLAN = $vmvlan
          $VMinfo.Notes = $vm.Notes
          $Report += $VMInfo
    }
    $Report = $Report | Sort-Object VMName
    IF ($Report -ne "") {
    $report | Export-Csv $ExportFilePath -NoTypeInformation
    }


    Looks like the URL you linked here  All VM's with VLAN's is broken after the switch to Broadcom, do you have an working link for both standard and distributed switches?




  • 6.  RE: How to get vm vlan-id into script ?

    Posted Dec 02, 2024 08:09 AM

    Yes, you are right, that should be the VMHost property, not the Host property.

    The latest migration did really mess up the links.
    I couldn't find that specific thread anymore, but I suspect it might have been the same code I used in Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch? | Automation



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


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


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



  • 7.  RE: How to get vm vlan-id into script ?

    Posted Dec 02, 2024 09:24 AM

    Found another mistake above:

    $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)  <----- This is returning 0 for all results.

    Looks like the mistake is $vm.HardDisks should be $VMInfo.DiskGb

    However, it looks like this is breaking something else, could you help me to understanding the part I underlined after the pipe?

    $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)




  • 8.  RE: How to get vm vlan-id into script ?

    Posted Dec 02, 2024 10:01 AM

    Sure, it summarises all the values in the CapacityKB property for all HardDisks.
    Then it converts that sum (in KB) to GB.

    So the reference to $vm.HardDisks is correct



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


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


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



  • 9.  RE: How to get vm vlan-id into script ?

    Posted Dec 03, 2024 02:44 AM

    Thanks LucD!




  • 10.  RE: How to get vm vlan-id into script ?

    Posted Dec 03, 2024 09:25 AM
    Edited by a_p_ Dec 03, 2024 10:26 AM

    Hi LucD,

    After making some changes to the script above to include a field called lastmodified date everything is working, but the script is running extremely slowly. I'm almost certain it's the way I'm getting the Datastore, specifically this line:

    Datastore = (($Datastores = Get-Datastore) | select Name, Id | where {$_.ID -match ((Get-View $vm).Datastore | Select Value).Value} | Select Name).Name      


    The Script: 

    Get-VM -PipelineVariable vm |
        ForEach-Object {
            Get-NetworkAdapter -VM $vm -PipelineVariable vnic |
        ForEach-Object {
            [PSCustomObject] @{
            Name = $vm.Name
            Powersate = $vm.Powerstate
            GuestOS = $vm.Guest.OSFullName
            Cluster = $vm.VMhost.Parent.Name
            VMHostName = $vm.VMHost.Name
            IP = ($vm.Guest.Nics | Where-Object {$_.Device.Name -eq $vnic.Name}).IPAddress -join ','
            MAC = $vnic.MacAddress
            NetworkName = $vnic.NetworkName
            Datastore = (($Datastores = Get-Datastore) | select Name, Id | where {$_.ID -match ((Get-View $vm).Datastore | Select Value).Value} | Select Name).Name        
            NumCPU = $vm.NumCPU
            MEMGB = [Math]::Round(($vm.MemoryGB),2)
            DiskGB = [Math]::Round($vm.ProvisionedSpaceGB,2)
            VLanID = & {
                if ($vnic.NetworkName) {
                    Get-VirtualPortGroup -Name $vnic.NetworkName -VM $vm |
                    ForEach-Object -Process {
                        if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
                            if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
                                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
                            } elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
                                if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
                                    [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))
                                } else {
                                    $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                                }
                            }
                        }
                        else {
                            $_.VlanId
                        }
                    }
                }
            }
            NicType = $vnic.Type
            Notes = $vm.Notes
            LastActiveDate = if ($Vm.PowerState -eq "PoweredOn") {
                "ACTIVE"
                }
                else {
                $vm.ExtensionData.Storage.Timestamp
                }
            }
        }
    }


    Is there a better/faster way to pull this information?




  • 11.  RE: How to get vm vlan-id into script ?

    Posted Dec 03, 2024 10:54 AM

    Try with

    Datastore = (Get-View -Id $vm.DatastoreIdList -Property Name).Name -join '|'


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


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


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



  • 12.  RE: How to get vm vlan-id into script ?

    Posted Dec 03, 2024 11:53 AM

    This is faster, thanks LucD.