for this script, I am trying to get some info on vms. Name, DC, Cluster.
But why am I getting multiple same lines of same VM?
$report = @()
foreach ($dc in Get-Datacenter) {
foreach ($cluster in Get-Cluster -Location $dc){
$vms = get-content "c:\scripts\vms.txt
foreach ($vm in $vms){
$info = "" | select Name, Datacenter, Cluster
$info.Name = $vm.name
$info.datacenter = $dc.name
$info.cluster = $cluster.name
$info.vcenter = $cluster.Uid.Split(":")[0].Split("@")[1]
$report += $info
}
}
}
$report | out-gridview
Does this also produce duplicates?
ForEach-Object -Process {
New-Object -TypeName PSObject -Property ([ordered]@{
Name = $_.Name
Datacenter = (Get-Datacenter -VM $_).Name
Cluster = (Get-Cluster -VM $_).Name
vCenter = ([uri]($_.ExtensionData.Client.ServiceUrl)).Host
})
} | Out-GridView
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
this works. but I wonder why the 1st script produces duplicates
To be honest, I would be amazed if that 1st script works at all.
You are reading a number of strings from a .txt file into $vms.
Then in the loop (foreach($vm in $vms)), you are getting the name property ($vm.Name) of each string.
A string doesn't have a Name property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference