VMware Cloud Community
CTPat
Contributor
Contributor

Inventory Script Adjust IPAdress and MAC in seperate Columns

I found an inventory script that works so far, but I want an adjustment and don't know how to do it. I want the IP address and MAC address in a separate column. And not like now in the script in the same column.

#### Comment out unneeded lines down below to make this run faster!! ####

#########################################################################

$StopWatch = [system.diagnostics.stopwatch]::startNew()

Write-Host " "

Write-Host "Loading snapin and connecting to vcenter.domain.com - please wait..."

#Add-PSSnapin -Name VMware.VimAutomation.Core

Connect-VIServer esxvca01.camiontransport.local -WarningAction SilentlyContinue

$allLines = @()

$VMsNotFoundFile = "C:\Temp\VMsNotFound.txt"

$CSVOutputFile = "C:\Temp\VMreport.csv"

###########################################################################

## Uncomment these lines to use only the VMs listed in a text file with this layout:

##    vmname1

##    vmname2

##    vmname3

##    ...

##

# Begin section for reading VMs from text file:

# (Comment out section below (all VMs) when using this.)

    #$InputFile = 'C:\Temp\vmnames.txt'

#    Write-Host " "

#    Write-Host "OK, now reading in server info. (This could take a while if you have more than a few servers.)"

#    $null | Out-File $VMsNotFoundFile

#    $reader = New-Object IO.StreamReader $InputFile

#     while($reader.ReadLine() -ne $null){ $TotalVMs++ }

    

#    Get-Content -Path $InputFile | %{

#        $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue

## End section for VMs in text file.

###########################################################################

###########################################################################

## Begin lines for *all* VMs in vCenter:

## (Comment out above section (VMs from text) when using this.)

    Write-Host " "

    Write-Host "OK. Now counting VMs - stand by..."

    $TotalVMs = Get-VM

    $TotalVMsCount = $TotalVMs.count

    Write-Host " "

    Write-Host "Here we go! Collecting server info... (Go get a cup of coffee - this will take a while...)"

    Get-VM | `

    ForEach-Object {

    $VM = $_

## End lines for all VMs in vCenter.

###########################################################################

    #$i++

    #$percentdone = (($i / $TotalVMs) * 100)

    #$percentdonerounded = "{0:N0}" -f $percentdone

    #Write-Progress -Activity "Gathering data from VMs" -CurrentOperation "Working on VM: $VM (VM $i of $TotalVMs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone

    $VMview = $VM | Get-View

    $VMResourceConfiguration = $VM | Get-VMResourceConfiguration

    IF (!$vm){

        Write-Host "$_ can't be found in VMware!"

        $_ | Out-File $VMsNotFoundFile -Append

        }

        else

        {

        #$VMHardDisks = $VM | Get-HardDisk

        #$HardDisksSizesGB = @()

        #$Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }

        #$VmdkSizeGB = ""

        #$Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }

        #$VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")

        #$TotalHardDisksSizeGB = 0

        #$Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }

        #$VMDKnames = @()

        #$Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }

       

        $Snapshots = $VM | Get-Snapshot

        $Report = "" | Select-Object VMname,Powerstate,vCPUcount,IPaddresses,MAC,SnapshotCount,GuestOS

        $Report.VMName = $VM.name

       # $Report.ESXname = $VM.VMHost

       # $Report.Host = $vm.VMHost.name

       # $Report.ClusterName = ($VM | Get-Cluster).Name

        $Report.Powerstate = $vm.Powerstate

       # $Report.OSBooted = (Invoke-VMScript -VM $VM -ScriptText 'systeminfo| find "System Boot Time"' -ScriptType Bat -ErrorAction SilentlyContinue | Select -ExpandProperty ScriptOutput) -replace 'System Boot Time:          ([^-]+)', '$1'

        #$Report.MemoryGB = $VM.MemoryMB/1KB

        $Report.vCPUcount = $VM.NumCpu

        #$Report.vNICcount = $VM.Guest.Nics.Count

        $Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)

        $Report.MAC = [string]::Join ('

       # $Report.VMXname = $VMview.Config.Files.VmPathName.Split("/")[1]

       # $Report.VMDKname = [string]::Join(',',$VMDKnames)

       # $Report.VmdkSizeGB = $VmdkSizeGB

       # $Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB

       # $Report.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)

       # $Report.DiskUsed = $Report.TotalVmdkSizeGB - $Report.DiskFree

       # $Report.DatastoreName = [string]::Join(',',($VMview.Config.DatastoreUrl | %{$_.Name}))

       # $Report.ToolsVersion = $VMview.Config.Tools.ToolsVersion

       # $Report.ToolsUpdate = $VMview.Guest.ToolsStatus

       # $Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares

       # $Report.CpuLimitMHZ = $VMResourceConfiguration.CpuLimitMhz

       # $Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz

       # $Report.NumMemShares = $VMResourceConfiguration.NumMemShares

       # $Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB

       # $Report.LimitMB = $VMResourceConfiguration.MemLimitMB

        $Report.SnapshotCount = (@($VM | Get-Snapshot)).Count

        $Report.GuestOS = $VM.Guest.OSFullName

       # $Report.HardwareVersion = $VM.Version

        #$tmpVLANId = Get-VirtualPortgroup -VM $vm | %{$_.VlanId}

        #if(!$tmpVLANId){$tmpVLANId = ""}

        #$Report.VLANid = [string]::Join(',',$tmpVLANId)

        #$tmpPG = Get-VirtualPortgroup -VM $vm | %{$_.Name}

        #if(!$tmpPG){$tmpPG = ""}

        #$Report.Portgroup = [string]::Join(',',$tmpPG)

        #$Report.Notes = $VM | Select-Object -ExpandProperty Notes

        #If (($Report.AnnoBackEx = ($VM | Get-Annotation -CustomAttribute "BackupExcluded") -replace 'BackupExcluded:([^-]+)', '$1') -eq "BackupExcluded:") { $Report.AnnoBackEx = "" } else { $Report.AnnoBackEx = ($VM | Get-Annotation -CustomAttribute "BackupExcluded") -replace 'BackupExcluded:([^-]+)', '$1' }

        #If (($Report.AnnoBackWeek = ($VM | Get-Annotation -CustomAttribute "BackupWeekly") -replace 'BackupWeekly:([^-]+)', '$1') -eq "BackupWeekly:") { $Report.AnnoBackWeek = "" } else { $Report.AnnoBackWeek = ($VM | Get-Annotation -CustomAttribute "BackupWeekly") -replace 'BackupWeekly:([^-]+)', '$1' }

        #If (($Report.AnnoBusiness = ($VM | Get-Annotation -CustomAttribute "Business") -replace 'Business:([^-]+)', '$1') -eq "Business:") { $Report.AnnoBusiness = "" } else { $Report.AnnoBusiness = ($VM | Get-Annotation -CustomAttribute "Business") -replace 'Business:([^-]+)', '$1' }

        #If (($Report.AnnoChgMgmt = ($VM | Get-Annotation -CustomAttribute "ChangeMgmt") -replace 'ChangeMgmt:([^-]+)', '$1') -eq "ChangeMgmt:") { $Report.AnnoChgMgmt = "" } else { $Report.AnnoChgMgmt = ($VM | Get-Annotation -CustomAttribute "ChangeMgmt") -replace 'ChangeMgmt:([^-]+)', '$1' }

        #If (($Report.AnnoCostCtr = ($VM | Get-Annotation -CustomAttribute "Cost Center") -replace 'Cost Center:([^-]+)', '$1') -eq "Cost Center:") { $Report.AnnoCostCtr = "" } else { $Report.AnnoCostCtr = ($VM | Get-Annotation -CustomAttribute "Cost Center") -replace 'Cost Center:([^-]+)', '$1' }

        #If (($Report.AnnoBLAHBLAH = ($VM | Get-Annotation -CustomAttribute "BLAHBLAH") -replace 'BLAHBLAH:([^-]+)', '$1') -eq "BLAHBLAH:") { $Report.AnnoBLAHBLAH = "" } else { $Report.AnnoBLAHBLAH = ($VM | Get-Annotation -CustomAttribute "BLAHBLAH") -replace 'BLAHBLAH:([^-]+)', '$1' }

        $allLines += $Report

    }

}

$allLines | Export-Csv $CSVOutputFile -NoTypeInformation

Write-Host " "

$StopWatch.Elapsed

Write-Host " "

Write-Host "Done! See above for elapsed time. Go look in C:\Temp for the files."

Tags (1)
Reply
0 Kudos
4 Replies
scott28tt
VMware Employee
VMware Employee

Moderator: Thread moved to the PowerCLI area.


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

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership

I'm not sure where you saw a MAC address in the report, the calculation for the MAC property is missing.

Try replacing the 2 lines with

$Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)

$Report.MAC = (Get-NetworkAdapter -VM $VM).MacAddress -join ','


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

Reply
0 Kudos
CTPat
Contributor
Contributor

Hey LucD

Thank that works great i have one issue IpAdresses gets now ipv4 and ipv6 out, is possible that only ipv4 are showed?

Thanks so far for help.

Reply
0 Kudos
LucD
Leadership
Leadership

Sure, you can use a Where-clause

$Report.IPaddresses = [string]::Join(',',($VM.Guest.IPAddress | where{$_ -notmatch "::"}))


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

Reply
0 Kudos