VMware Cloud Community
servicedeskLowa
Contributor
Contributor

Export to CSV adjustment

Dear all,

i have found a beautiful export script for all my machine but the company also want to see on what datacenter the vm is on. can someone please extend the script below with the option datacenter?

thanks in advanced!

<#

     Create .csv report with Virtual Machine Tag, Category, VMware tools version and Vm Notes.

#>

Get-Module -Name VMware* -ListAvailable | Import-Module

connect-viserver *************

#Create vmInfo object

$vmInfo = @()

    $vmInfoTemp = New-Object "PSCustomObject"

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMName -Value ""

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name ToolsVersion  -Value ""

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name HWVersion  -Value ""

    $vmCategories  = Get-TagCategory

    $vmCategories | %{$vmInfoTemp | Add-Member -MemberType NoteProperty -Name $_.Name  -Value "" }

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMNotes  -Value ""

    $vmInfo += $vmInfoTemp

get-vm | %{

   $vmInfoTemp = New-Object "PSCustomObject"

   $toolsVersion = Get-VMGuest $_ | select -ExpandProperty ToolsVersion

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMName -Value $_.Name

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name ToolsVersion  -Value $toolsVersion

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name HWVersion  -Value $_.Version

   $vmtags = ""

   $vmtags = Get-TagAssignment -Entity $_

   if($vmtags){

       $vmCategories | %{

           $tempVMtag = ""

            $tempCategroy = $_.Name

            $tempVMtag = $vmtags | Where-Object {$_.tag.category.name -match $tempCategroy}

            if($tempVMtag)

            {

                           $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $tempCategroy -Value $tempVMtag.tag.name

            }else {

                $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $tempCategroy -Value ""

            }

       }

   }else{

       $vmCategories | %{

           $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $_.name -Value ""

        }

   }

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMNotes  -Value $_.notes

   $vmInfo += $vmInfoTemp

}

$vmInfo | select * -Skip 1 | Export-Csv D:\Reports\VMware-Tools-version-Backup-export\VMware-Tools-version-Backup-export.csv -NoTypeInformation

0 Kudos
6 Replies
diegodco31
Leadership
Leadership

Hi

I prefer to use the rvtools to this actions.

https://www.robware.net/rvtools/

Diego Oliveira
LinkedIn: http://www.linkedin.com/in/dcodiego
0 Kudos
servicedeskLowa
Contributor
Contributor

thanks but this is not exactly what i am looking for, i need a export from all vm`s with the minimum if information of: Names, Tags, Datacenter, Tools versions and HWVersion.. so my script in my first post does almost all of them but it is only missing the datacenter.. i need this in CSV for reporting to the management monthly

0 Kudos
diegodco31
Leadership
Leadership

Hi

I understanded. The rvtools you can export the informations to the excel and filter informations.

Diego Oliveira
LinkedIn: http://www.linkedin.com/in/dcodiego
0 Kudos
servicedeskLowa
Contributor
Contributor

sorry rvtools is not an option, it is not showing me the tags that are assign`t to the machine and i want to run the script automatically by scheduled task.. can someone please extend my script? thanks in advanced!

0 Kudos
sjesse
Leadership
Leadership

Look at

Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.

you may be able to use that one and then just add the parts from this one you need. The one you have from what I've seen you can't modify your easily and its going to take some work to get everything you need in the other one. You can try moving your post to the powercli section as the people watching that might be able to help

0 Kudos
sjesse
Leadership
Leadership

I haven't tested this but I think should work, I don't have a place to test it right now. I think if the host is not in a cluster, you would need to remove the cluster part and change the location part of the hosts from the cluster to the dc.

$vmInfo = @()

    $vmInfoTemp = New-Object "PSCustomObject"

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMName -Value ""

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMDatacenter -Value ""

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name ToolsVersion  -Value ""

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name HWVersion  -Value ""

    $vmCategories  = Get-TagCategory

    $vmCategories | %{$vmInfoTemp | Add-Member -MemberType NoteProperty -Name $_.Name  -Value "" }

    $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMNotes  -Value ""

    $vmInfo += $vmInfoTemp

foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

get-vm  -Location $esx | %{

   $vmInfoTemp = New-Object "PSCustomObject"

   $toolsVersion = Get-VMGuest $_ | select -ExpandProperty ToolsVersion

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMName -Value $_.Name

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMDatacenter -Value $dc.name

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name ToolsVersion  -Value $toolsVersion

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name HWVersion  -Value $_.Version

   $vmtags = ""

   $vmtags = Get-TagAssignment -Entity $_

   if($vmtags){

       $vmCategories | %{

           $tempVMtag = ""

            $tempCategroy = $_.Name

            $tempVMtag = $vmtags | Where-Object {$_.tag.category.name -match $tempCategroy}

            if($tempVMtag)

            {

                           $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $tempCategroy -Value $tempVMtag.tag.name

            }else {

                $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $tempCategroy -Value ""

            }

       }

   }else{

       $vmCategories | %{

           $vmInfoTemp | Add-Member -MemberType NoteProperty -Name $_.name -Value ""

        }

   }

   $vmInfoTemp | Add-Member -MemberType NoteProperty -Name VMNotes  -Value $_.notes

   $vmInfo += $vmInfoTemp

}

}

}

}

$vmInfo | select * -Skip 1 | Export-Csv D:\Reports\VMware-Tools-version-Backup-export\VMware-Tools-version-Backup-export.csv -NoTypeInformation

0 Kudos