VMware Cloud Community
R0Ge
Contributor
Contributor
Jump to solution

how to create one vm-info report from several scripts?

Hello,

I have combined serveral scripts to the following and it ceates 4 correct outputfiles.

But Is it possible to create 1outputfile with all the rows?.

Thanks, Robert

  1. ************* Initialization Section ***********************************

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

  1. Declare global and static variables

  2. VMware VirtualCenter server name

$vcserver="Sxxxx"

  1. Path to output file

$outputfile1="D:\Output_scripts\VMware\VMinfo_part1.csv"

$outputfile2="D:\Output_scripts\VMware\VMinfo_part2.csv"

$outputfile3="D:\Output_scripts\VMware\VMinfo_part3.csv"

$outputfile4="D:\Output_scripts\VMware\VMinfo_part4.csv"

$outputfile_total="D:\Output_scripts\VMware\VMinfo_Total.csv"

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

  1. Add VI-toolkit

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

Add-PSsnapin VMware.VimAutomation.Core

Initialize-VIToolkitEnvironment.ps1

connect-VIServer $vcserver

  1. ************* Start Process 1 *********************************************

$report1 = @()

Get-VM | % {

$vm = $_ | Get-VMGuest

$row = "" | Select VMname, VMTools, IPAddr1, IPAddr2, IPAddr3, OsFullName, Hostname

$row.VMname = $_.Name

$row.VMTools = $vm.State

$row.Hostname = $vm.Hostname

$row.IPAddr1 = $vm.IPAddress[0]

$row.IPAddr2 = $vm.IPAddress[1]

$row.IPAddr3 = $vm.IPAddress[3]

$row.OsFullName = $vm.osfullname

$report1 += $row

}

$report1 | sort -property VMname | Export-Csv $outputfile1 -NoTypeInformation

  1. ************* Start Process 2 *********************************************

$Report2 = @()

get-vm | % {

$vm = Get-View $_.ID

$row = "" | Select-Object VMname, VMState, TotalNics, TotalCPU, "TotalMemory Mb"

$row.VMname = $vm.Name

$row.VMState = $vm.summary.runtime.powerState

$row.TotalNics = $vm.summary.config.numEthernetCards

$row.TotalCPU = $vm.summary.config.numcpu

$row.{TotalMemory Mb}= $vm.summary.config.memorysizemb

$report2 += $row

}

$report2 | sort -property VMname | Export-Csv $outputfile2 -NoTypeInformation

  1. ************* Start Process 3 *********************************************

$report3 = @()

get-vm | % {

$vm = $_

$disksize = (($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum) / 1Mb

$_ | Get-Datastore | Where-Object {$_.Type -eq "VMFS"} | % {

$row = "" | Select VMname, Aantaldisks, Datastore, "Total Disk GB"

$row.VMname = $vm.Name

$row.Aantaldisks = $vm.Harddisks.length

$row.Datastore = $_.Name

$row.{Total Disk GB} = $disksize

$report3 += $row

}

}

$report3 | sort -property VMname |Export-Csv $outputfile3 -noTypeInformation

  1. ************* Start Process 4 *********************************************

$ROTAP=@{ Name="ROTAP"; Expression={$_.CustomFields.Item("ROTAP")}}

$User=@{ Name="User"; Expression={$_.CustomFields.Item("User")}}

$Description=@{ Name="Description"; Expression={$_.CustomFields.Item("Description")}}

Get-VM | Select-Object -property "name", $ROTAP,$User,$Description | sort -property Name | Export-Csv $outputfile4 -noTypeInformation

  1. ************* End Process ***********************************************

DisConnect-VIServer -confirm:$false

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The 4 Export-Csv cmdlets each write a specific array to the CSV file.

Each of the arrays has different properties, or columns if you want to analogy with the CSV file.

Since you can't have a CSV file with different columns this will not be possible afaik.

An alternative could be that you collect the all the information in one row that has all the properties.

That way you will end up with 1 array which you can export to 1 CSV file.

The following is how this could be done for the first 2 CSV files

Get-VM | % {
	$vmGuest = $_ | Get-VMGuest
	$vm = Get-View $_.ID
	
	$row = "" | Select VMname, VMTools, IPAddr1, IPAddr2, IPAddr3, OsFullName, Hostname, 
	                   VMState, TotalNics, TotalCPU, "TotalMemory Mb"
	$row.VMname = $_.Name
	$row.VMTools = $vmGuest.State
	$row.Hostname = $vmGuest.Hostname
	$row.IPAddr1 = $vmGuest.IPAddress[0]
	$row.IPAddr2 = $vmGuest.IPAddress[1]
	$row.IPAddr3 = $vmGuest.IPAddress[3]
	$row.OsFullName = $vmGuest.osfullname

	$row.VMState = $vm.summary.runtime.powerState
	$row.TotalNics = $vm.summary.config.numEthernetCards
	$row.TotalCPU = $vm.summary.config.numcpu
	$row.{TotalMemory Mb}= $vm.summary.config.memorysizemb

	$report += $row
}
$report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation

Since the basic loop of each report is the loop through all the guests (Get-VM cmdlet) it should quite easy to incorporate the other 2 CSV files in a similar way.


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The 4 Export-Csv cmdlets each write a specific array to the CSV file.

Each of the arrays has different properties, or columns if you want to analogy with the CSV file.

Since you can't have a CSV file with different columns this will not be possible afaik.

An alternative could be that you collect the all the information in one row that has all the properties.

That way you will end up with 1 array which you can export to 1 CSV file.

The following is how this could be done for the first 2 CSV files

Get-VM | % {
	$vmGuest = $_ | Get-VMGuest
	$vm = Get-View $_.ID
	
	$row = "" | Select VMname, VMTools, IPAddr1, IPAddr2, IPAddr3, OsFullName, Hostname, 
	                   VMState, TotalNics, TotalCPU, "TotalMemory Mb"
	$row.VMname = $_.Name
	$row.VMTools = $vmGuest.State
	$row.Hostname = $vmGuest.Hostname
	$row.IPAddr1 = $vmGuest.IPAddress[0]
	$row.IPAddr2 = $vmGuest.IPAddress[1]
	$row.IPAddr3 = $vmGuest.IPAddress[3]
	$row.OsFullName = $vmGuest.osfullname

	$row.VMState = $vm.summary.runtime.powerState
	$row.TotalNics = $vm.summary.config.numEthernetCards
	$row.TotalCPU = $vm.summary.config.numcpu
	$row.{TotalMemory Mb}= $vm.summary.config.memorysizemb

	$report += $row
}
$report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation

Since the basic loop of each report is the loop through all the guests (Get-VM cmdlet) it should quite easy to incorporate the other 2 CSV files in a similar way.


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

0 Kudos
R0Ge
Contributor
Contributor
Jump to solution

Hi LucD

I solved it with the following script as you suggest.. This is what I am was looking for.

All the info with a basic loop and one output file.

Thanks

                          • Start Process *********************************************

$report = @()

$ROTAP=@{ Name="ROTAP"; Expression={$_.CustomFields.Item("ROTAP")}}

$User=@{ Name="User"; Expression={$_.CustomFields.Item("User")}}

$Description=@{ Name="Description"; Expression={$_.CustomFields.Item("Description")}}

Get-VM | % {

$vmGuest = $_ | Get-VMGuest

$vm = Get-View $_.ID

$disksize = (($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum) / 1Mb

$DataStore = ($_ | Get-Datastore | Select-Object -property 'Name')

$row = "" | Select VMname, VMTools, IPAddr1, IPAddr2, IPAddr3, OsFullName, Hostname,

VMState, TotalNics, TotalCPU, "TotalMemory Mb", Numberdisks, Datastore, "Total Disk GB", ROTAP, User, Description

$row.VMname = $_.Name

$row.VMTools = $vmGuest.State

$row.Hostname = $vmGuest.Hostname

$row.IPAddr1 = $vmGuest.IPAddress[0]

$row.IPAddr2 = $vmGuest.IPAddress[1]

$row.IPAddr3 = $vmGuest.IPAddress[3]

$row.OsFullName = $vmGuest.osfullname

$row.VMState = $vm.summary.runtime.powerState

$row.TotalNics = $vm.summary.config.numEthernetCards

$row.TotalCPU = $vm.summary.config.numcpu

$row.{TotalMemory Mb}= $vm.summary.config.memorysizemb

$row.Numberdisks = $_.Harddisks.length

$row.Datastore = $DataStore.Name

$row.{Total Disk GB} = $disksize

$row.ROTAP = $_.CustomFields.Item("ROTAP")

$row.User = $_.CustomFields.Item("User")

$row.Description = $_.CustomFields.Item("Description")

$report += $row

}

$report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation

                          • End Process ***********************************************

0 Kudos