VMware Cloud Community
blueten
Contributor
Contributor
Jump to solution

Utilizing Get-View to export results to CSV file

I'm connecting to multiple vCenters and obtaining the vm's and attempting to export results from Get-View to a CSV file. The only results that I get when I run my script is the headers.

Could someone point out what I'm doing wrong in the code:

#CSV Path and file name to be created

$ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

#Connect to the multiple vCenters

Write-Host "Connecting to vCenters" -ForegroundColor Cyan

$VIServers = Get-Content -Path C:\temp\vcenters.txt

$c = Get-Credential

$VIServers = Connect-VIServer $VIServer -Credential $c  -warningaction 0

ForEach ($VIServer in $VIServers) {

    Write-Host "Gathering Statistics for VMs in $VIServer"

    $table = @()

    $vm = Get-VM

    ForEach ($vm in $VMs) {

        $Report = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

        $Report.VIServer = $_.VIServer

        $Report.Name = (get-View $vm.Name)

        $Report.DNS_Name = (get-View $vm.Guest.HostName)

        $Report.OS = (get-View $vm.Guest.GuestFullName)

        $Report.PowerState = (get-View $vm.Summary.Runtime.powerstate)

        $Report.Template_Status = (get-View $vm.Summary.Config.Template)

        $Report.IPAddress = (get-View $vm.Summary.IpAddress)

       $Report.LastBootTime = (get-View $vm.Summary.Runtime.BootTime)

        $Report.Notes = (get-View $vm.config.annotation)

        $table += $Report

       }

}

$table | Export-Csv $ExportFilePath -NoTypeInformation

Disconnect-VIServer -Confirm:$False

Thanks so much!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with this version

#CSV Path and file name to be created

$ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

#Connect to the multiple vCenters

Write-Host "Connecting to vCenters" -ForegroundColor Cyan

$VIServerNames = Get-Content -Path C:\temp\vcenters.txt

$c = Get-Credential

$VIServers = Connect-VIServer -Server $VIServerNames -Credential $c  -warningaction 0


$table = @()


ForEach ($VIServer in $global:DefaultVIServers) {

   Write-Host "Gathering Statistics for VMs in $($VIServer.Name)"

   $vms = Get-VM -Server $VIServer

   ForEach ($vm in $VMs) {

   $row = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

   $row.VIServer = $VIServer.Name

   $row.Name = $vm.Name

   $row.DNS_Name = $vm.Guest.ExtensionData.HostName

   $row.OS = $vm.Guest.OSFullName

   $row.PowerState = $vm.PowerState

   $row.Template_Status = $vm.ExtensionData.Summary.Config.Template

   $row.IPAddress = $vm.Guest.IpAddress -join '|'

   $row.LastBootTime = $vm.ExtensionData.Summary.Runtime.BootTime

   $row.Notes = $vm.ExtensionData.config.annotation

   $table += $row

   }

}

$table | Export-Csv $ExportFilePath -NoTypeInformation

Disconnect-VIServer -Confirm:$False


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

You should probably initialise the resulting $table outside the loop.
The resulting VMs per vCenter should be collected in $vms, not $vm.
Not sure why you have a Get-View for most properties, this can also be reached via the ExtensionData property.

#CSV Path and file name to be created

$ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

#Connect to the multiple vCenters

Write-Host "Connecting to vCenters" -ForegroundColor Cyan

$VIServers = Get-Content -Path C:\temp\vcenters.txt

$c = Get-Credential

$VIServers = Connect-VIServer $VIServer -Credential $c  -warningaction 0


$table = @()


ForEach ($VIServer in $VIServers) {

   Write-Host "Gathering Statistics for VMs in $VIServer"

   $vm = Get-VM -Server $VIServer

   ForEach ($vm in $VMs) {

   $row = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

   $row.VIServer = $VIServer.Name

   $row.Name = $vm.Name

   $row.DNS_Name = $vm.Guest.HostName

   $row.OS = $vm.Guest.GuestFullName

   $row.PowerState = $vm.PowerState

   $row.Template_Status = $vm.ExtensionData.Summary.Config.Template

   $row.IPAddress = $vm.ExtensionData.Summary.IpAddress

   $row.LastBootTime = $vm.ExtensionData.Summary.Runtime.BootTime

   $row.Notes = $vm.ExtensionData.config.annotation

   $table += $row

   }

}

$table | Export-Csv $ExportFilePath -NoTypeInformation

Disconnect-VIServer -Confirm:$False


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

0 Kudos
blueten
Contributor
Contributor
Jump to solution

Thanks so much for the quick reply.

In response to "Not sure why you have a Get-View for most properties". It's because I'm a total novice with scripting and trying to make some progress.  aka rookie  Smiley Wink

The CSV file is now returning without any data. Not sure why. I've been trying to resolve it but not having any luck.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with this version

#CSV Path and file name to be created

$ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

#Connect to the multiple vCenters

Write-Host "Connecting to vCenters" -ForegroundColor Cyan

$VIServerNames = Get-Content -Path C:\temp\vcenters.txt

$c = Get-Credential

$VIServers = Connect-VIServer -Server $VIServerNames -Credential $c  -warningaction 0


$table = @()


ForEach ($VIServer in $global:DefaultVIServers) {

   Write-Host "Gathering Statistics for VMs in $($VIServer.Name)"

   $vms = Get-VM -Server $VIServer

   ForEach ($vm in $VMs) {

   $row = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

   $row.VIServer = $VIServer.Name

   $row.Name = $vm.Name

   $row.DNS_Name = $vm.Guest.ExtensionData.HostName

   $row.OS = $vm.Guest.OSFullName

   $row.PowerState = $vm.PowerState

   $row.Template_Status = $vm.ExtensionData.Summary.Config.Template

   $row.IPAddress = $vm.Guest.IpAddress -join '|'

   $row.LastBootTime = $vm.ExtensionData.Summary.Runtime.BootTime

   $row.Notes = $vm.ExtensionData.config.annotation

   $table += $row

   }

}

$table | Export-Csv $ExportFilePath -NoTypeInformation

Disconnect-VIServer -Confirm:$False


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

0 Kudos
blueten
Contributor
Contributor
Jump to solution

That's worked.

Thanks so much for your help. I really appreciate it!!

Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're welcome.
If you have further questions, feel free to ask Smiley Happy


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

0 Kudos