Hey Guys,
Why is this script not working.
I dont get the array populated with the host names and versions.
Get-view -ViewType HostSystem -Property Name, Config.Product | select Name, {$_.Config.Product.version} | %{if($_.Config.Product.FullName -eq "3.5.0")
{ $vmarray += $_ }
I want to then do a foreach on that vmarray as following.
Foreach($esx in $vmarray){
$parent = $esx.Name.ExtensionData.Parent
$loc = Get-View $parent
while($loc.getType().Name -ne "Datacenter"){
$parent = $loc.Parent
$loc = Get-View $parent
}
$loc.Name
}
which gives me the datacenter name as well where the host is - finally i will push these to a csv.
Where is the script screwing up?
Thanks
RJ
Ok to start you are checking the fullname to see if it equals 3.5.0, shouldnt that be a check on the version or are you trying to do something else ?
To adjust your code so it works you can do this:
$vmarray = Get-view -ViewType HostSystem | Where {$_.Config.Product.Version -eq "3.5.0" }
Foreach($esx in $vmarray){
$parent = $esx.Parent
$loc = Get-View $parent
while($loc.getType().Name -ne "Datacenter"){
$parent = $loc.Parent
$loc = Get-View $parent
}
$loc.Name
}
Im not sure that gives you exactly what you want though, wouldnt you want to output the HostName and DataCenter for each object ?
Thanks, this is the working code finally.
$vmarray=@()
$object=@()
Connect-VIServer -Server vcenter -Protocol https -Credential $creds
$vmarray = Get-view -ViewType HostSystem -Property Name, Config.Product | where {$_.Config.product.version -eq "3.5.0"}
for($i=0; $i -lt $vmarray.length; $i++){
$dcname = get-datacenter -vmhost $vmarray[$i].Name | select name
$object += New-Object PSObject -Property @{
Hostname = $vmarray[$i].Name
Datacenter = $dcname.Name
}
}
$Object | Export-Csv C:\Users\thakur.ratan\Desktop\esxversion.csv
ok, I assumed you were doing it this way for speed, there is a much easier way to do this...
Connect-VIServer -Server vcenter -Protocol https -Credential $creds
Get-VMHost | where {$_.Version -eq "3.5.0"} | Select Name, @{N="Datacenter";E={Get-Datacenter -VMHost $_}} | Export-Csv C:\Users\thakur.ratan\Desktop\esxversion.csv
Hello,
Yes indeed it was faster
How ever when i am doing a loop through all vc's it never exits
foreach($vc in $vcs){
Connect-VIServer -Server $vcs -Protocol https -Credential $creds
Get-VMHost | where {$_.Version -eq "3.5.0"} | Select Name, @{N="Datacenter";E={Get-Datacenter -VMHost $_}} | Export-Csv C:\Users\thakur.ratan\Desktop\esxversion.csv
}
How do i get that please.
Thanks
RJ
Looks to me like you still have your connection inside the foreach loop.