I'm trying to use some code from the community to read the notes section because we track VM expiration, usage, etc there. However my script is returning the name only and not the notes data. Thanks.
$VIServer = "esxhost1"
Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1
$date=get-date
Connect to the VI Server
Write-Host "Connecting to VI Server"
Connect-VIServer $VIServer
$VMs = Get-Inventory
$myCol = @()
$i = 0
ForEach ($VM in $VMs)
{
$i ++
$myObj = "" | Select-Object Name, FieldKey, FieldValue
$myObj.Name = $VM.Name
#$myObj.Summary = $VM.Summary
$myObj.FieldKey = $VM.CustomFields.Keys[$i]
$myObj.FieldValue = $VM.CustomFields.Values[$i]
$myCol += $myObj
$myObj
}
$myCol | Export-Csv "C:\VMware_Healthcheck\Reports\VMs_Notes.csv" -NoTypeInformation
Output:
Name | FieldKey | FieldValue |
base-rtp-rhes51-2C-20GB |
I suspect the problem comes form the fact that your $i variable starts with the value 1 and then is incremented with each guest.
While in my opinion it should restart from 0 with each guest.
Also note that you probably want to use the Get-VM cmdlet instead of the Get-Inventory cmdlet.
Try the following script and see if that lists your notes correctly
$VMs = Get-VM $myCol = @() $i = 0 foreach ($VM in $VMs) { for($i = 0; $i -lt $vm.CustomFields.Count; $i++){ $myObj = "" | Select-Object Name, FieldKey, FieldValue $myObj.Name = $VM.Name $myObj.FieldKey = $VM.CustomFields.Keys[$i] $myObj.FieldValue = $VM.CustomFields.Values[$i] $myCol += $myObj } } $myCol | Export-Csv "C:\VMware_Healthcheck\Reports\VMs_Notes.csv" -NoTypeInformation
In this script you will have a line in the CSV file for each note variable.
If you have a fixed number of note fields for each guest, the script can be adapted to display all the note fields on 1 line.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I had get-vm, but in my attempts to get a working script I didn't get it back to the original. I dropped in this code, but now it is not displaying anything.
$VMs = Get-VM
$myCol = @()
$i = 0
foreach ($VM in $VMs)
{
for($i = 0; $i -lt $vm.CustomFields.Count; $i++){
$myObj = "" | Select-Object Name, FieldKey, FieldValue
$myObj.Name = $VM.Name
$myObj.FieldKey = $VM.CustomFields.Keys[$i]
$myObj.FieldValue = $VM.CustomFields.Values[$i]
$myCol += $myObj
}
}
$myCol | Export-Csv "C:\VMware_Healthcheck\Reports\VMs_Notes.csv" -NoTypeInformation
Same thing when I move the $i increment to the bottom...
$VMs = Get-VM
$myCol = @()
$i = 0
ForEach ($VM in $VMs)
{
$myObj = "" | Select-Object Name, FieldKey, FieldValue
$myObj.Name = $VM.Name
#$myObj.Summary = $VM.Summary
$myObj.FieldKey = $VM.CustomFields.Keys[$i]
$myObj.FieldValue = $VM.CustomFields.Values[$i]
$myCol += $myObj
$i ++
$myObj
}
$myCol | Export-Csv "C:\VMware_Healthcheck\Reports\VMs_Notes.csv" -NoTypeInformation
That could mean you are actually using the notes field and not the custom Attributes field.
Where is the data located ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you want to extract the Notes you can use this script.
$VMs = Get-VM $myCol = @() foreach($vm in $VMs){ $myObj = "" | Select-Object Name, Notes $myObj.Name = $VM.Name $myObj.Notes = $VM.Description $myCol += $myObj } $myCol | Export-Csv "C:\VMware_Healthcheck\Reports\VMs_Notes.csv" -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That was it. Thanks!!!!!!!