1 2 Previous Next 15 Replies Latest reply on Sep 6, 2019 5:59 AM by LucD

    PCLI script for generating virtual machine inventory including custom annotations

    Bean78 Novice

      Can anyone help me with a PCLI script to generate VMware virtual machine inventory with includes even custom annotations created by us (Environment vCenter 6.5/6.7)

        • 1. Re: PCLI script for generating virtual machine inventory including custom annotations
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          Is that 1 custom attribute per VM, or multiple?
          And does each VM has the same number of custom attributes?

          • 2. Re: PCLI script for generating virtual machine inventory including custom annotations
            Bean78 Novice

            Multiple custom attributes are created and propagated to all virtual machines in the vCenter console.

            • 3. Re: PCLI script for generating virtual machine inventory including custom annotations
              asajm Hot Shot
              vExpert

              Hi Bean78

              I reached this earlier and you can edit it according to your requirements.

              Get-Datacenter -Name "Offsite DC" | Get-Folder -Name "BUILDS" | Get-VM |

              ForEach-Object {

                $VM = $_

                $VM | Get-Annotation |

                  ForEach-Object {

                 $Report = "" | Select-Object VM,Name,Value

                 $Report.VM = $VM.Name

                 $Report.Name = $_.Name

                 $Report.Value = $_.Value

                 $Report

                }

              } | Export-Csv -Path VMAnnotations.csv -NoTypeInformation -UseCulture

               

              To import the annotations from the "file".csv:

               

              Import-Csv -Path VMAnnotations.csv | Where-Object {$_.Value} | ForEach-Object {

                Get-VM $_.VM | Set-Annotation -CustomAttribute $_.Name -Value $_.Value

              }

              • 4. Re: PCLI script for generating virtual machine inventory including custom annotations
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                But is the number of custom attributes per VM always the same, or can that vary?

                • 6. Re: PCLI script for generating virtual machine inventory including custom annotations
                  Bean78 Novice

                  Thanks, but how do i run the same script across the entire vCenter.

                  • 7. Re: PCLI script for generating virtual machine inventory including custom annotations
                    LucD Guru
                    vExpertUser ModeratorsCommunity Warriors

                    Then try like this

                     

                    $report = @()

                    foreach($vm in Get-VM){

                       $row = "" | Select Name,State

                       $row.Name = $vm.Name

                       $row.State = $vm.PowerState

                       $vm.CustomFields | %{

                       Add-Member -InputObject $row -Name $_.Key -Value $_.Value -MemberType NoteProperty

                       }

                       $report  += $row

                    }


                    $report | Export-Csv ".\report.csv" -NoTypeInformation -UseCulture

                    • 8. Re: PCLI script for generating virtual machine inventory including custom annotations
                      Bean78 Novice

                      Thanks Lucd that worked, can you let me know if i need to add some more rows to the script to identify the values for the CPUs,memory, each Hard disk size, what need to be done, please assist on it

                      $row.CPUs = $vm.vCPU

                      $row.Memory Size = $vm.GB

                      $row.HDD1 Size = $vm.GB

                      $row.HDD2.Size = $vm.GB

                      • 9. Re: PCLI script for generating virtual machine inventory including custom annotations
                        LucD Guru
                        User ModeratorsvExpertCommunity Warriors

                        Try something like this

                         

                        $report = @()

                        foreach ($vm in Get-VM)

                        {

                           $row = "" | Select Name, State, NumCpu, MemoryGB, HD1, HD2

                           $row.Name = $vm.Name

                           $row.State = $vm.PowerState

                           $row.NumCpu = $vm.NumCpu

                           $row.MemoryGB = $vm.MemoryGB

                           $row.HD1 = (Get-HardDisk -VM $vm -Name 'Hard disk 1' -ErrorAction SilentlyContinue).CapacityGB

                           $row.HD2 = (Get-HardDisk -VM $vm -Name 'Hard disk 2' -ErrorAction SilentlyContinue).CapacityGB

                           $vm.CustomFields | % {

                           Add-Member -InputObject $row -Name $_.Key -Value $_.Value -MemberType NoteProperty

                           }

                           $report += $row

                        }

                        $report | Export-Csv ".\report.csv" -NoTypeInformation -UseCulture

                        • 10. Re: PCLI script for generating virtual machine inventory including custom annotations
                          Bean78 Novice

                          Excellent, appreciate it - the script worked as expected. Can you please clarify in the future, if i need to add some parameters like virtual machines VMTools status, vHardware version and UUID. Do you have a link where can i match these parameters and edit the script accordingly as per my requirement.

                          • 11. Re: PCLI script for generating virtual machine inventory including custom annotations
                            LucD Guru
                            Community WarriorsvExpertUser Moderators

                            One of the advantages of using PowerShell is that there are a lot of options available to discover what is available.

                            One way of finding what properties are there, pipe the output ot the Get-Member cmdlet.

                             

                            When you want to add a property in the above script, you would to first have to add to the initial " | Select ...

                            Then add a line where fill in the value of that property.

                             

                            The PowerCLI Cmdlet Reference lets you explore what cmdlets are there, but also what objects they return.

                            • 12. Re: PCLI script for generating virtual machine inventory including custom annotations
                              Bean78 Novice

                              I tried the following script by including ToolsStatus but it didnot provide me with a value. Between the HDD shows only the capacity but it did not show the utilization and can we have the custom attribute specific to these annotations "primary owner" and "secondary owner" and "applicaiton name". Can you assist me on this...

                               

                              $report = @()

                              foreach ($vm in Get-VM)

                              {

                                 $row = "" | Select Name, State, NumCpu, MemoryGB, ToolsStatus, EVCName, NumUUID, HD1, HD2

                                 $row.Name = $vm.Name

                                 $row.State = $vm.PowerState

                                 $row.NumCpu = $vm.NumCpu

                                 $row.MemoryGB = $vm.MemoryGB

                                 $row.ToolsStatus = $vm.ToolsStatus

                                 $row.HD1 = (Get-HardDisk -VM $vm -Name 'Hard disk 1' -ErrorAction SilentlyContinue).CapacityGB

                                 $row.HD2 = (Get-HardDisk -VM $vm -Name 'Hard disk 2' -ErrorAction SilentlyContinue).CapacityGB

                                 $vm.CustomFields | % {

                                 Add-Member -InputObject $row -Name $_.Key -Value $_.Value -MemberType NoteProperty

                                 }

                                 $report += $row

                              }

                              $report | Export-Csv "C:\Temp\report1.csv" -NoTypeInformation -UseCulture

                              • 13. Re: PCLI script for generating virtual machine inventory including custom annotations
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                Those are quite a bit of additional requirements since your original question.

                                 

                                The usage per harddisk is not monitored directly by vSphere.

                                To obtain it, other methods need to be used. See for example Yadr – A Vdisk Reporter.

                                 

                                With your additional requirements, but without the HD usage, the script becomes.

                                 

                                $report = @()

                                foreach ($vm in Get-VM)

                                {

                                   $hd1 = Get-HardDisk -VM $vm -Name 'Hard disk 1' -ErrorAction SilentlyContinue

                                   $hd2 = Get-HardDisk -VM $vm -Name 'Hard disk 2' -ErrorAction SilentlyContinue


                                   $row = "" | Select Name, State, NumCpu, MemoryGB, ToolsStatus, EVCName, NumUUID, HD1CapacityGB, HD2CapacityGB,

                                   'Primary Owner','Secondary Owner','Applicaiton Name'

                                   $row.Name = $vm.Name

                                   $row.State = $vm.PowerState

                                   $row.NumCpu = $vm.NumCpu

                                   $row.MemoryGB = $vm.MemoryGB

                                   $row.ToolsStatus = $vm.Guest.State

                                   $row.HD1CapacityGB = $hd1.CapacityGB

                                   $row.HD2CapacityGB = $hd2.CapacityGB

                                   $row.'Primary Owner' = (Get-Annotation -Entity $vm -Name 'Primary Owner').Value

                                   $row.'Secondary Owner' = (Get-Annotation -Entity $vm -Name 'Secondary Owner').Value

                                   $row.'Applicaiton Name' = (Get-Annotation -Entity $vm -Name 'Applicaiton Name').Value

                                   $report += $row

                                }


                                $report | Export-Csv "C:\Temp\report1.csv" -NoTypeInformation -UseCulture

                                • 14. Re: PCLI script for generating virtual machine inventory including custom annotations
                                  Bean78 Novice

                                  I get the following error but the actual custom annotation in the vCenter looks like 'App Pri Owner' and 'App Sec Owner' and i have changed the annotation in the script with the space and with quotes but its the same error. 

                                   

                                  $report = @()

                                  foreach ($vm in Get-VM)

                                  {

                                     $hd1 = Get-HardDisk -VM $vm -Name 'Hard disk 1' -ErrorAction SilentlyContinue

                                     $hd2 = Get-HardDisk -VM $vm -Name 'Hard disk 2' -ErrorAction SilentlyContinue

                                     $row = "" | Select Name, State, NumCpu, MemoryGB, ToolsStatus, HD1CapacityGB, HD2CapacityGB, AppPriOwner, AppSecOwner

                                     $row.Name = $vm.Name

                                     $row.State = $vm.PowerState

                                     $row.NumCpu = $vm.NumCpu

                                     $row.MemoryGB = $vm.MemoryGB

                                     $row.ToolsStatus = $vm.Guest.State

                                     $row.HD1CapacityGB = $hd1.CapacityGB

                                     $row.HD2CapacityGB = $hd2.CapacityGB

                                     $row.AppPriOwner = (Get-Annotation -Entity $_ -Name AppPriOwner).Value

                                     $row.AppSecOwner = (Get-Annotation -Entity $_ -Name AppSecOwner).Value

                                     $report += $row

                                  }

                                  $report | Export-Csv "C:\Temp\report1.csv" -NoTypeInformation -UseCulture

                                   

                                  Get-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the argument, and then try running the command again.

                                  At line:28 char:47

                                  +    $row.AppPriOwner = (Get-Annotation -Entity $_ -Name AppPriOwner).V ...

                                  +                                               ~~

                                      + CategoryInfo          : InvalidData: (:) [Get-Annotation], ParameterBindingValidationException

                                      + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetAnnotation

                                   

                                  Get-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the argument, and then try running the command again.

                                  At line:30 char:47

                                  +    $row.AppSecOwner = (Get-Annotation -Entity $_ -Name AppSecOwner).V ...

                                  +                                               ~~

                                      + CategoryInfo          : InvalidData: (:) [Get-Annotation], ParameterBindingValidationException

                                      + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetAnnotation

                                  1 2 Previous Next