VMware Cloud Community
JoeH09
Contributor
Contributor
Jump to solution

Getting Notes field from PowerCli

I'm using $VMNotes = Get-VM myvm | Select-Object Notes in a script to pull the contents of the Notes field.  The returned data is:

@{Notes=my notes

}

How do I get it to return just the contents of the notes field, without the @{Notes= }?

Tags (1)
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Try $VMNotes = Get-VM myvm | Select-Object -ExpandProperty Notes

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
16 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Try $VMNotes = Get-VM myvm | Select-Object -ExpandProperty Notes

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
JoeH09
Contributor
Contributor
Jump to solution

That did it.  Thanks!

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Hey Guys

any idea what am i missing here ?

the vm.notes attribute is only available if i remove the vm-guest filter

Thanks

Martin

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} | get-vmguest)) {

   

    write-host $vm.Hostname $vm.IPAddress $vm.OSFullName $Datacenter.name $Vm.Notes

   

  }

 

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The VMGuest objects doesn't have the Notes property.

You could do

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"}) {

        Get-VMGuest -VM $vm |

        Select Hostname,IPAddress,OSFullName@{N='Datacenter';E={$Datacenter.name}},@{N='Notes';E={$Vm.Notes}}

  }

}


Or (the VirtualMachine object is available under the VM property).

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} | Get-VMGuest) {

        Write-Host $vm.Hostname $vm.IPAddress $vm.OSFullName $Datacenter.name $Vm.VM.Notes

  }

}


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

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Thanks Buddy !


"$Vm.VM.Notes" did it for me...

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Hey Luc

do you know why the csv output has just strings with  .Hashtable+KeyCollection ?

i remember running a sort-table after the pipe to get the csv data normalized, but i cannot get it working

since i need both console and csv outputs

Thanks

Martin

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} |get-vmguest)) {   

     write-host $vm.Hostname $vm.NumCPU $vm.IPAddress $vm.OSFullName $Datacenter.name $Vm.VM.Notes

    @{N="VM_NAME#";E={$vm.Hostname}},

    @{N="VM_CPU_Core#";E={$vm.NumCPU}},

    @{N="VM_IP#";E={$vm.IPAddress}},

    @{N="VM_OS";E={$vm.OSFullName}},

    @{N="VM_DC";E={$Datacenter.name}},

    @{N="VM_NOTES";E={$Vm.VM.Notes}}

    }

 

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "d:\temp\report.csv"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} |get-vmguest)) {  

        $vm | Select @{N="VM_NAME#";E={$vm.Hostname}},

            @{N="VM_CPU_Core#";E={$vm.NumCPU}},

            @{N="VM_IP#";E={$vm.IPAddress}},

            @{N="VM_OS";E={$vm.OSFullName}},

            @{N="VM_DC";E={$Datacenter.name}},

            @{N="VM_NOTES";E={$Vm.VM.Notes}}

     }

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "d:\temp\report.csv"

 


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

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Thanks Luc !

i added some trim at 30 chars on the "notes" since it was messing up with the formatting

here is the working version in case anyone is interested

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM  |get-vmguest)) { 

        $vm | Select @{N="DEVICE_NAME";E={$vm.Hostname}},

        @{N="VM_VM_LABEL";E={$vm.VMname}},

        @{N="DEVICE_CPU_STATE";E={$vm.vm.PowerState}},   

            @{N="DEVICE_CPU_COUNT";E={$vm.VM.NumCPU}},   

        @{N="DEVICE_CPU_MEM_GB";E={$vm.VM.MemoryGB}},

        @{N="DEVICE_CPU_PROVISIONED_DISK";E={[math]::round($VM.vm.ProvisionedSpaceGB)}},

            @{N="DEVICE_IP";E={$vm.IPAddress}},

            @{N="DEVICE_OS";E={$vm.OSFullName}},

            @{N="DEVICE_DC";E={$Datacenter.name}},

            @{N="APPLICATION";E={$Vm.VM.Notes.remove(30)}}

     }

}

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Hi Luc

would you know by any chance what is the proper way to get to the notes inside the VMhost (not vm's) ?

i need to use get-view to get some additional properties, but cant seem to find the proper way to

query the notes of the ESX itself

if i query get-vmhost |select customfields, they are there, but they dont show up under the view

any ideas ?

Thanks

Martin

$ESXInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

        ForEach ($Cluster in ($Datacenter | Get-Cluster  | Sort-Object -Property Name)) {

         ForEach ($ESX in ($Cluster | Get-VMHost |Sort Name |Get-View )) {

         $ESX | Select @{N="DEVICE_NAME";E={$ESX.name}},

       @{N="DEVICE_STATE";E={$_.Summary.Runtime.PowerState}},

       @{N="DEVICE_MANUFACTURER";E={$_.Hardware.SystemInfo.Vendor}},

       @{N="DEVICE_MODEL";E={$_.Hardware.SystemInfo.Model}},

       @{N="DEVICE_VERSION";E={$_.Config.Product.Version}},

       @{N=“DEVICE_CPU_COUNT“;E={$_.Hardware.CpuInfo.NumCpuPackages}},

       @{N=“DEVICE_CPU_MEM_GB“;E={“” + (([math]::round($_.Hardware.MemorySize / 1GB, 0))* 1024/1024)}},

       @{N="DEVICE_CPU_PROVISIONED_DISK";E={}},

       @{N=“DEVICE_IP“;E={($_.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}},

       @{N=“DEVICE_OS“;E={$_.Config.Product.Name + $_.Config.Product.Version + “ - Build “ + $_.Config.Product.Build}},

       @{N="DEVICE_NOTES_TEST";E={$ESX.value.Notes}},

       @{N="DEVICE_NOTES_2";E={$ESX.CustomFields.Item("Notes")}}

     }

  }

}

$ESXInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\temp\vmhost.csv"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

They should be under $esx.Summary.CustomValue


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

Reply
0 Kudos
MartinTor
Contributor
Contributor
Jump to solution

Hey Luc,

when doing a query on this one

$esx.Summary.CustomValue i get VMware.Vim.CustomFieldStringValue on the csv

am i missing anything ? i tried to call all properties on this attribute

but cant seem to get this right...i can get the results, outside of the get-view

but not using results within get-view

maybe its cause its Friday...any ideas ?

Thx

Martin

Reply
0 Kudos
lorried82
Enthusiast
Enthusiast
Jump to solution

Hi LucD - This is pretty much what I am looking for but I can't get it to work. I am looking for a report that will pull all my VM's and provide the details for Name, OS, Notes, Tag, DataCenter and Cluster. The script you have below is close, however I am not sure how to get tags and cluster in it - and the export is not working either. Any help?

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} |get-vmguest)) { 

        $vm | Select @{N="VM_NAME#";E={$vm.Hostname}},

            @{N="VM_CPU_Core#";E={$vm.NumCPU}},

            @{N="VM_IP#";E={$vm.IPAddress}},

            @{N="VM_OS";E={$vm.OSFullName}},

            @{N="VM_DC";E={$Datacenter.name}},

            @{N="VM_NOTES";E={$Vm.VM.Notes}}

     }

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "d:\temp\report.csv"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$VmInfo = ForEach ($Datacenter in (Get-Datacenter)) {

     ForEach ($VM in ($Datacenter | Get-VM | where {$_.powerstate -match "on"} | Get-VMGuest)) {

        $vm | Select @{N="VM_NAME#";E={$vm.Hostname}},

            @{N="VM_CPU_Core#";E={$VM.VM.NumCPU}},

            @{N="VM_IP#";E={$vm.IPAddress}},

            @{N="VM_OS";E={$vm.OSFullName}},

            @{N="VM_DC";E={$Datacenter.name}},

            @{N='VM_Cluster';E={(Get-Cluster -VM $VM.VM).Name}},

            @{N="VM_NOTES";E={$VM.VM.Notes}},

            @{N='VM_Tags';E={(Get-TagAssignment -Entity $VM.VM).Tag.Name -join '|'}}

     }

}

$VmInfo | Export-Csv -Path "d:\temp\report.csv" -NoTypeInformation -UseCulture


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

kbhowmick123
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I have the below script running, however when ever I'm trying to add the nodes field it's not working.

Get-VM | Sort-Object -property Name | Get-View -Property @("Name", "Guest.GuestFullName", "Guest.IPAddress", "VM.Notes") | Select -Property Name, @{N="Running-OS";E={$_.Guest.GuestFullName}},@{N="ip",E={$_.Guest.IPAddress -join '|'}}N,"notes descrp";E={$_.Guest.VM.Notes}} | Export-Csv -Path c:\temp\report.csv

When I'm adding notes I'm getting error.

Could you please help me on this.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since you are using Get-View the calculated property should be

@{N='notes descrp';E={$_.Config.Annotation}}


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

Reply
0 Kudos
teklone
Contributor
Contributor
Jump to solution

Correction: 

 

Get-VM | Sort-Object -property Name | Get-View -Property @("Name", "Guest.GuestFullName", "Guest.IPAddress", "Config.Annotation") | Select -Property Name, @{N="Running-OS";E={$_.Guest.GuestFullName}},@{N="ip";E={$_.Guest.IPAddress -join '|'}},@{N="notes descrp";E={$_.Config.Annotation}}

Reply
0 Kudos