VMware Cloud Community
JLogan2016
Enthusiast
Enthusiast
Jump to solution

Get-View in a nested loop

I have been looking at a script to pull in some information regarding templates. This was not written by me, but I am trying to get it working. The script has nested For loops in it, which seem to be fine with the majority of items I am pulling back. For information I am pulling out of Get-View, however, the results are not consistent. The code is below:

      $datacenters = Get-Datacenter

        foreach($Datacenter in $Datacenters) {

            $TemplateNames = Get-Template -Location $Datacenter           

            foreach ($template in $TemplateNames) {

                 $templateview = Get-View -id (Get-Template $template).Id   

                 write-host DataCenter ---- $Datacenter

                 write-host Template Name ----  $Template.Name

                 write-host OS Version  ---- $templateview.Summary.Config.GuestFullName

                 pause

                 write-host Template Notes  ---- $templateview.Summary.Config.Annotation

                 write-host vCPU  ---- $Template.ExtensionData.Config.Hardware.NumCPU

                 write-host Memory ----  $Template.ExtensionData.Config.Hardware.MemoryMB    

                 write-host Number of Hard Disks  ---- ($Template | Get-HardDisk | Measure-Object).Count

                 write-host HD Name  ---- ($template|Get-HardDisk).Name

                 write-host HD Size GB  ---- $template|Get-HardDisk|select -ExpandProperty CapacityGB

                 write-host HD Format  ---- ($template|Get-HardDisk).storageformat

                 write-host VM Hardware Version ----  ($templateview).config.version

                 write-host VM Tools Version ----  $templateview.config.tools.toolsversion

                 write-host VM Tools  ---- $template.ExtensionData.Guest.ToolsVersionStatus

                 pause

                }

         }

For any item using the $templateview info, it duplicates the data returned:

OS Version ---- Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft W

indows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft

Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microso

ft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Micro

soft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Mic

rosoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) M

icrosoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit) Microsoft Windows 7 (64-bit)

VM Hardware Version ---- vmx-11 vmx-08 vmx-11 vmx-08 vmx-11 vmx-11 vmx-08 vmx-10 vmx-11 vmx-08 vmx-11 vmx-11 vmx-08 vmx-08 vmx-10 vmx-08 vmx-11

vmx-10 vmx-08 vmx-07 vmx-10 vmx-10 vmx-10 vmx-08 vmx-11 vmx-10 vmx-09 vmx-08 vmx-08 vmx-08 vmx-08 vmx-08 vmx-11

But only for the first loop. Subsequent iterations return the information as I would expect:

OS Version ---- Microsoft Windows Server 2008 R2 (64-bit)

VM Hardware Version ---- vmx-11

Any idea why this would be happening only the first loop? I don't see it being related to the OS, as a subsequent desktop template works fine as well.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can't replicate what you are seeing.

Did you already try to stop/start your PowerShell session?

But there is no need for the Get-View, you can do that via the ExtensionData property, which you are already using in some lines.

Try like this

foreach($Datacenter in Get-Datacenter) { 

    foreach ($template in Get-Template -Location $Datacenter) { 

         write-host DataCenter ---- $Datacenter 

         write-host Template Name ----  $Template.Name  

         write-host OS Version  ---- $template.ExtensionData.Summary.Config.GuestFullName 

         write-host Template Notes  ---- $template.ExtensionData.Summary.Config.Annotation  

         write-host vCPU  ---- $Template.ExtensionData.Config.Hardware.NumCPU  

         write-host Memory ----  $Template.ExtensionData.Config.Hardware.MemoryMB      

         write-host Number of Hard Disks  ---- ($Template | Get-HardDisk | Measure-Object).Count  

         write-host HD Name  ---- ($template|Get-HardDisk).Name  

         write-host HD Size GB  ---- $template|Get-HardDisk|select -ExpandProperty CapacityGB  

         write-host HD Format  ---- ($template|Get-HardDisk).storageformat  

         write-host VM Hardware Version ----  $template.ExtensionData.config.version  

         write-host VM Tools Version ----  $template.ExtensionData.config.tools.toolsversion  

         write-host VM Tools  ---- $template.ExtensionData.Guest.ToolsVersionStatus 

    }

}

  


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Can't replicate what you are seeing.

Did you already try to stop/start your PowerShell session?

But there is no need for the Get-View, you can do that via the ExtensionData property, which you are already using in some lines.

Try like this

foreach($Datacenter in Get-Datacenter) { 

    foreach ($template in Get-Template -Location $Datacenter) { 

         write-host DataCenter ---- $Datacenter 

         write-host Template Name ----  $Template.Name  

         write-host OS Version  ---- $template.ExtensionData.Summary.Config.GuestFullName 

         write-host Template Notes  ---- $template.ExtensionData.Summary.Config.Annotation  

         write-host vCPU  ---- $Template.ExtensionData.Config.Hardware.NumCPU  

         write-host Memory ----  $Template.ExtensionData.Config.Hardware.MemoryMB      

         write-host Number of Hard Disks  ---- ($Template | Get-HardDisk | Measure-Object).Count  

         write-host HD Name  ---- ($template|Get-HardDisk).Name  

         write-host HD Size GB  ---- $template|Get-HardDisk|select -ExpandProperty CapacityGB  

         write-host HD Format  ---- ($template|Get-HardDisk).storageformat  

         write-host VM Hardware Version ----  $template.ExtensionData.config.version  

         write-host VM Tools Version ----  $template.ExtensionData.config.tools.toolsversion  

         write-host VM Tools  ---- $template.ExtensionData.Guest.ToolsVersionStatus 

    }

}

  


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

Reply
0 Kudos
JLogan2016
Enthusiast
Enthusiast
Jump to solution

Thanks! I will try this out. I will also try the script on another machine and see if I can replicate.

Edit: I was able to replicate. Nevertheless, your suggestion was a lot more straightforward. Thanks again

Reply
0 Kudos