VMware Cloud Community
Bean78
Contributor
Contributor

PCLI script for generating virtual machine inventory including custom annotations

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)

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
Bean78
Contributor
Contributor

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

Reply
0 Kudos
asajm
Expert
Expert

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

}

If you think your queries have been answered
Marking this response as "Solution " or "Kudo"
ASAJM
Reply
0 Kudos
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
Bean78
Contributor
Contributor

its always the same

Reply
0 Kudos
Bean78
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
Bean78
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
Bean78
Contributor
Contributor

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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.


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

Reply
0 Kudos
Bean78
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
Bean78
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

That should be $vm instead of $_


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

Reply
0 Kudos