VMware Cloud Community
lorried82
Enthusiast
Enthusiast

Script to pull VM info

Hello - I am looking to get a script to pull the following VM Info

VM Name

Full OS (not sure if I can get kernel)

Data Center Name

Cluster Name

VM Host

CPU (count and speed)

Memory

Network Adapters Name

Network Adapter Type

Network Adapter NetworkName

IP Address

Notes

Hard Disks with DataStores, disk format and Sizes

this is the script I found and have been modifying but I am not getting all the details (nor am I consistently getting the OS, IPAddr, etc

Any help is greatly appreciated.

filter Get-FolderPath {

    $_ | Get-View | % {

        $row = "" | select Name, Path

        $row.Name = $_.Name

        $current = Get-View $_.Parent

#        $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path

        $path = ""

        do {

            $parent = $current

            if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}

            $current = Get-View $current.Parent

        } while ($current.Parent -ne $null)

        $row.Path = $path

        $row

    }

}

$ExportFilePath = "z:\VMInfo.csv"

$Report = @()

$VMs = Get-VM vmname

$adapters =Get-NetworkAdapter $VMs | select Name,Type,NetworkName

$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,Folder,IPAddress,NetworkAdapter,VLAN,Host,Cluster,Datastore,NumCPU,MemMb,Notes

        $VMInfo.VMName = $vm.name

        $VMInfo.Powerstate = $vm.Powerstate

        $VMInfo.OS = $vm.Guest.OSFullName

        $VMInfo.Folder = ($vm | Get-Folderpath).Path

        $VMInfo.IPAddress = $vm.Guest.IPAddress[0]

        $VMInfo.NetworkAdapter = $vm.adapters.Name

        $VMInfo.VLAN = $vm.adapters.NetworkName

        $VMInfo.Host = $vm.host.name

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

$VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name

        $VMInfo.NumCPU = $vm.NumCPU

        $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)

        $VMInfo.notes = $vm.notes

        $Report += $VMInfo

}

$Report = $Report | Sort-Object VMName

IF ($Report -ne "") {

$report | Export-Csv $ExportFilePath -NoTypeInformation

}

0 Kudos
3 Replies
ccalvetTCC
Enthusiast
Enthusiast

Hi,

A VM is associated from zero to many network cards.

A VM is associated from zero to many disks.

For all others properties this is a one to one relation. (or no relation with a cluster if the VM is located on a standalone host)

Name/FullOS/DataCenter/Cluster/Host/CPU (For the VM or the ESXi?)/Memory/Notes

So before starting to work on a script, what do you expect from your script?
What will be the format of the report?
Are you planning to import the data in another IT system?

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos
lorried82
Enthusiast
Enthusiast

I expect my script to just be a report reading the information I am looking for.

I would like this to be in a csv file and this data is not going to be imported anywhere.

I understand there can be many NIC's, Datastores, and IP's and I think that is where I am struggling to get the data. Can you help with that? Here is my most recent edit of the script - I am not sure if it is as efficient as it could be as well. I am able to pull all my data besides the datastores or harddisk, I am also not getting the IP's or OS for all the VM's but some of them - which seems odd. Thanks

filter Get-FolderPath {

    $_ | Get-View | % {

        $row = "" | select Name, Path

        $row.Name = $_.Name

        $current = Get-View $_.Parent

#        $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path

        $path = ""

        do {

            $parent = $current

            if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}

            $current = Get-View $current.Parent

        } while ($current.Parent -ne $null)

        $row.Path = $path

        $row

    }

}

$ExportFilePath = "z:\VMInfo.csv"

$Report = @()

$VMs = Get-VM vmname

$adapters =Get-NetworkAdapter $VMs | select Name,Type,NetworkName

$Datastores = Get-Datastore | select Name, Id

$harddisk =Get-harddisk $VMs| select Name, Parent, StorageFormat, FileName, CapacityGB

$VMHosts = Get-VMHost | select Name, Parent

ForEach ($VM in $VMs) {

        $VMView = $VM | Get-View

        $VMInfo = {} | Select

VMName,OS,IPAddress,NetworkAdapter,NICType,VLAN,Host,Datacenter,Cluster,Disk,DatastoreName,Format,Capacity,NumCPU,MemMb

,Notes

        $VMInfo.VMName = $vm.name

        $VMInfo.OS = $vm.Guest.OSFullName

        $VMInfo.IPAddress = $vm.guest.IPAddress[0]

        $VMInfo.NetworkAdapter = ($vm| get-networkadapter).Name

        $VMInfo.NICType = ($vm | get-networkadapter).type

        $VMInfo.VLAN = ($vm | get-networkadapter).Networkname

        $VMInfo.Host = $vm.host.name

        $VMInfo.Datacenter = ($vm | Get-Folderpath).Path

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

        $VMInfo.Disk = ($harddisk | ).Name

        $VMInfo.DatastoreName = ($vm | get-harddisk).FileName

        $VMInfo.Format = ($vm | get-harddisk).storageformat

        $VMInfo.Capacity = ($vm | get-harddisk).capacityGB

        $VMInfo.NumCPU = $vm.NumCPU

        $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)

        $VMInfo.notes = $vm.notes

        $Report += $VMInfo

}

$Report = $Report | Sort-Object VMName

IF ($Report -ne "") {

$report | Export-Csv $ExportFilePath -NoTypeInformation

}

0 Kudos
ccalvetTCC
Enthusiast
Enthusiast

Hi,

You will find below different approaches. (List non-exhaustive)

Option 1:
Create three separate CSV files

One for all properties in a one to one relation

One for all disk properties

One for all network properties


Pro:

Easy to create a script

Report easy to read

Con:

3 Files

Option 2:
One XML file

Pro:
One file

Con:

Script more challenging to create
Report not easy to read

Option 3:
Everything in one CSV file with only three columns.

Example
VM / Property / Value

VM1 / Memory / 1024

VM1 / Network Card 1 Type / E1000
VM1 / Network Card 1 Network / VMPortGroup50

Pro:
One file

Still readable but not easy to compare values between VMs in one report
Con:
Script more challenging to create

Which report do you think is the most relevant for your needs?

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos