VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

get-vm cluster name

any idea why I am getting an error here? Trying to get the cluster name as wellScreen Shot 2017-08-22 at 9.09.35 AM.png

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Filter is a hash table, so you can just add other entries like with a regular hash table.

Try like this

$report = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType  VirtualMachine -Filter  @{'Guest.ToolsStatus'="toolsNotInstalled|toolsNotRunning|toolsOld";

                                                           'Runtime.PowerState'='poweredOn'}

      foreach ($vm in $vms){

        $info = "" | select Name, Datacenter, Cluster, Toolsstatus, guestos, vcenter

        $info.Name = $vm.name

        $info.datacenter = $dc.name

        $info.cluster = $cluster.name

        $info.toolsstatus = $vm.guest.toolsstatus

        $info.guestos = $vm.guest.guestfullname

        $info.vcenter = $cluster.Uid.Split(":")[0].Split("@")[1]

        $report += $info

      }

    }

}

$report | export-csv "c:\scripts\oldvmtools.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Can't see the error, but it looks as if you calculated property for the cluster doesn't start with a @


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

hiu lucd. yes that was it. but not when i export-csv

I only see 3 VMs

but when I dont export, there are tons of VMs with old vmtools

does export-csv have a limitation

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, it doesn't.

Can you share the complete code, I can see all of it in the screenshot.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

here you go lucd

Screen Shot 2017-08-23 at 6.54.57 AM.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're overwriting the CSV file for each cluster, the Export-Csv is inside the ForEach loop.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

ok I see that

thanks

Can you also help get the vcenter info on this?  $info.vcenter

I cannot seem to get the right syntax

Also can it take a very long time to runScreen Shot 2017-08-23 at 8.41.09 AM.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you please attach the code as text, and not as a screenshot?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

here it is

$report = @()

foreach ($dc in get-datacenter) {

    foreach ($cluster in get-cluster -Location $dc){

    $vms = get-vm -Location $cluster | ? { $_.powerstate -eq "PoweredOn" -and $_.extensiondata.guest.toolsstatus -ne "toolsok"}

    foreach ($vm in $vms){

    $vmview = get-vm -Name $vm -Location $cluster | Get-View

    $info = "" | select Name, Datacenter, Cluster, Toolsstatus, guestos, vcenter

    $info.Name = $vm.name

    $info.datacenter = $dc.name

    $info.cluster = $cluster.name

    $info.toolsstatus = $vmview.guest.toolsstatus

    $info.guestos = $vmview.guest.guestfullname

    $info.vcenter = $vm | select @{N="vcenter";E={$Uid.Split(":")[0].Split("@")[1]}}

    $report += $info

    }}}

    $report | export-csv "c:\scripts\oldvmtools.csv" -notypeInformation

0 Kudos
tdubb123
Expert
Expert
Jump to solution

can this be replaced with

Get-view -viewtype  virtualmachine -Filter  @{'Guest.ToolsStatus'="toolsNotInstalled|toolsNotRunning|toolsOld"}  | select Name,@{N='tools';E={$_.Guest.ToolsStatus}

how do I specify multiple filters?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Filter is a hash table, so you can just add other entries like with a regular hash table.

Try like this

$report = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType  VirtualMachine -Filter  @{'Guest.ToolsStatus'="toolsNotInstalled|toolsNotRunning|toolsOld";

                                                           'Runtime.PowerState'='poweredOn'}

      foreach ($vm in $vms){

        $info = "" | select Name, Datacenter, Cluster, Toolsstatus, guestos, vcenter

        $info.Name = $vm.name

        $info.datacenter = $dc.name

        $info.cluster = $cluster.name

        $info.toolsstatus = $vm.guest.toolsstatus

        $info.guestos = $vm.guest.guestfullname

        $info.vcenter = $cluster.Uid.Split(":")[0].Split("@")[1]

        $report += $info

      }

    }

}

$report | export-csv "c:\scripts\oldvmtools.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

thanks Lucd.

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Hi Luc

something does not seem right. I am seeing multiple entries of the same server in different DCs when i run the script. But there is only one server in one DC location.

why is it reporting the same server multiple times in different DCs/clusters?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because the Get-View doesn't limit itself to the cluster, like the Location parameter does with your Get-Cluster cmdlet.

With Get-View you can use the SearchRoot parameter instead.

Like this

$report = @()

foreach ($dc in Get-Datacenter) {

    foreach ($cluster in Get-Cluster -Location $dc){

      $vms = Get-view -ViewType  VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef `

          -Filter  @{'Guest.ToolsStatus'="toolsNotInstalled|toolsNotRunning|toolsOld";

                      'Runtime.PowerState'='poweredOn'}

      foreach ($vm in $vms){

        $info = "" | select Name, Datacenter, Cluster, Toolsstatus, guestos, vcenter

        $info.Name = $vm.name

        $info.datacenter = $dc.name

        $info.cluster = $cluster.name

        $info.toolsstatus = $vm.guest.toolsstatus

        $info.guestos = $vm.guest.guestfullname

        $info.vcenter = $cluster.Uid.Split(":")[0].Split("@")[1]

        $report += $info

      }

    }

}

$report | export-csv "c:\scripts\oldvmtools.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos