Automation

 View Only
Expand all | Collapse all

get-vm cluster name

  • 1.  get-vm cluster name

    Posted Aug 22, 2017 04:11 PM

    any idea why I am getting an error here? Trying to get the cluster name as well



  • 2.  RE: get-vm cluster name

    Posted Aug 22, 2017 04:50 PM

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



  • 3.  RE: get-vm cluster name

    Posted Aug 23, 2017 04:05 AM

    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



  • 4.  RE: get-vm cluster name

    Posted Aug 23, 2017 06:09 AM

    No, it doesn't.

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



  • 5.  RE: get-vm cluster name

    Posted Aug 23, 2017 01:56 PM

    here you go lucd



  • 6.  RE: get-vm cluster name

    Posted Aug 23, 2017 02:39 PM

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



  • 7.  RE: get-vm cluster name

    Posted Aug 23, 2017 03:43 PM

    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 run



  • 8.  RE: get-vm cluster name

    Posted Aug 23, 2017 05:01 PM

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



  • 9.  RE: get-vm cluster name

    Posted Aug 23, 2017 08:37 PM

    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



  • 10.  RE: get-vm cluster name

    Posted Aug 23, 2017 09:11 PM

    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?



  • 11.  RE: get-vm cluster name
    Best Answer

    Posted Aug 23, 2017 09:41 PM

    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



  • 12.  RE: get-vm cluster name

    Posted Aug 23, 2017 10:56 PM

    thanks Lucd.



  • 13.  RE: get-vm cluster name

    Posted Aug 24, 2017 02:00 AM

    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?



  • 14.  RE: get-vm cluster name

    Posted Aug 24, 2017 07:11 AM

    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