Hi guys,
Trying to get an inventory with multiple vcenter connected,
Have some trouble with the output some VM are assigned to wrong Vcenter and running OS is not always correct.
Can someone help ?
Here the script :
$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {
ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Vcenter";E={$vcenter.Name}},
@{N="OS";E={$vm.Guest.OsFullName}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Cluster";E={$Cluster.Name}},
@{N="Host";E={$vm.VMHost.Name}},
@{N="HostVersion";E={$vm.VMHost.version}}
}
}
}
}
}
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"
Try like this.
Does that produce less lines ?
$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {
ForEach ($Datacenter in (Get-Datacenter -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk -Server $vcenter | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Vcenter";E={$vcenter.Name}},
@{N="OS";E={$vm.Guest.OsFullName}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Cluster";E={$Cluster.Name}},
@{N="Host";E={$vm.VMHost.Name}},
@{N="HostVersion";E={$vm.VMHost.version}}
}
}
}
}
}
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
the output i got was about 90000 lines ...
Don't know where is the mistake.:(
Try like this.
Does that produce less lines ?
$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {
ForEach ($Datacenter in (Get-Datacenter -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM -Server $vcenter | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk -Server $vcenter | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Vcenter";E={$vcenter.Name}},
@{N="OS";E={$vm.Guest.OsFullName}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Cluster";E={$Cluster.Name}},
@{N="Host";E={$vm.VMHost.Name}},
@{N="HostVersion";E={$vm.VMHost.version}}
}
}
}
}
}
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No one ?
thanks Lucd !
its working fine now, can you please explain the solution ?
thx !
Sure, by adding the Server parameter on the cmdlets, you restrict them to only return the objects for that specific vCenter.
Otherwise, when you are connected to multiple vSphere Server (vCenter and ESXi nodes) and you have PowerCLI configured for "Multiple" mode (see the Set-PowerCLIConfiguration cmdlet), the cmdlet will return objects for all these connections.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great ! thanks
LucD
LucDHow can we add other items such as RAM, CPU, HDD1, HDD2, HDD3, etc. Thank you!
Check this you will get complete vm information..this might helpfull for fetching information on large scale environment.
Thanks, Vineeth. That is a nice script as well. However, I'm still interested in LucD's response.
Cool
The thread mentioned by Vineeth has most of the addition info you want.
On the harddisk properties, do want these properties fixed (HD1, HD2 and HD3), even if a VM might no have 3 HDs ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I have many VMs with multiple HDDs. Thanks.
What I meant is do you want to have a variable number of columns in the output ?
A VM with 1 HDD will have less properties than on with 3 HDD.
Variable length arrays of objects tend to have an issue in Export-Csv, unless the array is sorted in descending number of properties order (longest entry at the top).
An alternative is to have a fixed number of properties, but then some of the will be blank.
How do you want to the resulting CSV file to look ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, I don't need a variable number of columns. I took a deeper look at Complete VM information and it looks to fit my needs. Thank you LucD & vin01!