VMware Cloud Community
ranjitcool
Hot Shot
Hot Shot

Why wont this work?

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

Please award points if you find my answers helpful Thanks RJ Visit www.rjapproves.com
0 Kudos
5 Replies
alanrenouf
VMware Employee
VMware Employee

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 ?

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
ranjitcool
Hot Shot
Hot Shot

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

Please award points if you find my answers helpful Thanks RJ Visit www.rjapproves.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee

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

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
ranjitcool
Hot Shot
Hot Shot

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

Please award points if you find my answers helpful Thanks RJ Visit www.rjapproves.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee

Looks to me like you still have your connection inside the foreach loop.

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos