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
************* Initialization Section ***********************************
##################################
Declare global and static variables
VMware VirtualCenter server name
$vcserver="Sxxxx"
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"
#################
Add VI-toolkit
#################
Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1
connect-VIServer $vcserver
************* 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
************* 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
************* 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
************* 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
************* End Process ***********************************************
DisConnect-VIServer -confirm:$false
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
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
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 ***********************************************