VMware Cloud Community
vincekwok
Contributor
Contributor
Jump to solution

Failed to get "notes" of virtual machine

I tried to use a powershell script to get some information of virtual machine. I can get VMHost, VMname and GuestVersion by using the script in the following, but failed to get notes of VM. Anyone know why I cannot get notes, please help me on this. Thank you in advance.

report = @()
foreach ($vmView in (Get-View -ViewType VirtualMachine))
{
    $report += $vmView | Select-Object @{Name="VMHost"; Expression={(Get-View $vmView.Runtime.Host).Name}},
                                       @{Name="VMName"; Expression={$_.Name}},
@{Name="Notes"; Expression={$_.Description}},

                                       @{Name="GuestVersion"; Expression={$vmView.Guest.GuestFullName}}
}
$report | Sort-Object VMName -Descending | Export-Csv "d:\vm_GuestVersion.csv" -NoTypeInformation

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is no need to do this with the Get-View cmdlet.

You can do

$report = @()
foreach ($vm in Get-VM)
{
   
$report += $vm | `
        Select-Object @{Name="VMHost"; Expression={($_.Host).Name}},
            @{Name
="VMName"; Expression={$_.Name}},
            @{Name
="Notes"; Expression={$_.Description}},
            @{Name
="GuestVersion"; Expression={$_.Guest.OSFullName}}
}
$report | Sort-Object VMName -Descending | Export-Csv "d:\vm_GuestVersion.csv" -NoTypeInformation

If you want to do this with the Get-View cmdlet for whatever reason, you can do

$report = @()
foreach ($vmView in (Get-View -ViewType VirtualMachine))
{
    $report += $vmView | `
       
Select-Object @{Name="VMHost"; Expression={(Get-View $vmView.Runtime.Host).Name}},             @{Name="VMName"; Expression={$_.Name}},             @{Name="Notes"; Expression={$_.Config.Annotation}},             @{Name="GuestVersion"; Expression={$vmView.Guest.GuestFullName}} } $report | Sort-Object VMName -Descending | Export-Csv "d:\vm_GuestVersion.csv" -NoTypeInformation

But note that since you require a Get-View each time for the hostname, the performance will suffer.


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

View solution in original post

0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

There is no need to do this with the Get-View cmdlet.

You can do

$report = @()
foreach ($vm in Get-VM)
{
   
$report += $vm | `
        Select-Object @{Name="VMHost"; Expression={($_.Host).Name}},
            @{Name
="VMName"; Expression={$_.Name}},
            @{Name
="Notes"; Expression={$_.Description}},
            @{Name
="GuestVersion"; Expression={$_.Guest.OSFullName}}
}
$report | Sort-Object VMName -Descending | Export-Csv "d:\vm_GuestVersion.csv" -NoTypeInformation

If you want to do this with the Get-View cmdlet for whatever reason, you can do

$report = @()
foreach ($vmView in (Get-View -ViewType VirtualMachine))
{
    $report += $vmView | `
       
Select-Object @{Name="VMHost"; Expression={(Get-View $vmView.Runtime.Host).Name}},             @{Name="VMName"; Expression={$_.Name}},             @{Name="Notes"; Expression={$_.Config.Annotation}},             @{Name="GuestVersion"; Expression={$vmView.Guest.GuestFullName}} } $report | Sort-Object VMName -Descending | Export-Csv "d:\vm_GuestVersion.csv" -NoTypeInformation

But note that since you require a Get-View each time for the hostname, the performance will suffer.


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

0 Kudos
vincekwok
Contributor
Contributor
Jump to solution

Thank you very much, your script works.

0 Kudos
DjinjiRinji
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Great code!! Could you help me inject that "get note" code into this script

Thank you.

script source: http://www.wooditwork.com/2010/08/16/exporting-all-that-useful-vm-information-with-powercli/

@"
===============================================================================
Title:         Export-VMInfo.ps1
Description:   Exports VM Information from vCenter into a .CSV file for importing into anything
Usage:         .\Export-VMInfo.ps1
Date:          04/03/2010
===============================================================================
"@
filter Get-FolderPath {
    $_ | Get-View | % {
        $row = "" | select Name, Path
        $row.Name = $_.Name
        $current = Get-View $_.Parent
#        $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
        $path = ""
        do {
            $parent = $current
            if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
            $current = Get-View $current.Parent
        } while ($current.Parent -ne $null)
        $row.Path = $path
        $row
    }
}
$VCServerName = "XXXXXXXXXX"
$VC = Connect-VIServer $VCServerName
$Datacenter = "XXXX"
$ExportFilePath = "XXXXXXXXX\Export-VMInfo.csv"
$Report = @()
$VMs = Get-Datacenter $Datacenter | Get-VM
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
      $VMView = $VM | Get-View
      $VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed
      $VMInfo.VMName = $vm.name
      $VMInfo.Powerstate = $vm.Powerstate
      $VMInfo.OS = $vm.Guest.OSFullName
      $VMInfo.Folder = ($vm | Get-Folderpath).Path
      $VMInfo.IPAddress = $vm.Guest.IPAddress[0]
      $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
      $VMInfo.Host = $vm.host.name
      $VMInfo.Cluster = $vm.host.Parent.Name
      $VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
      $VMInfo.NumCPU = $vm.NumCPU
      $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
      $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
      $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
      $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
      $Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False
Kindly mark as solved if your questions are answered.
>>>>>
Guillermo R
LinkedIn: https://www.linkedin.com/in/gramallo
Web: http://bakingclouds.com/
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure. I just show the lines that are needed.

    
 ...
$VMInfo = {} | Select VMName,Notes,Powerstate,OS,Folder,IPAddress,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed   
$VMInfo
.VMName = $vm.name
$VMInfo.Notes = $vm.Description
...


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

0 Kudos
DjinjiRinji
Enthusiast
Enthusiast
Jump to solution

great!! thanks guru Smiley Wink

Kindly mark as solved if your questions are answered.
>>>>>
Guillermo R
LinkedIn: https://www.linkedin.com/in/gramallo
Web: http://bakingclouds.com/
0 Kudos
nl26348
Contributor
Contributor
Jump to solution

Hi Luc

Export info script with notes works fine except that the note text after a semicolon would be skipped.
Do you have any idea why?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No clue.

Is the value complete in $vmInfo.Notes ?


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

0 Kudos
nl26348
Contributor
Contributor
Jump to solution

I've got the same result with $vmInfo.Notes

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you do

Get-VM | Select Name,Description | 
Export-Csv
c:\notes.csv -NoTypeInformation -UseCulture

do you get the complete Description in the CSV file ?

I just tried, and I can't reproduce the problem.


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

0 Kudos
nl26348
Contributor
Contributor
Jump to solution

Yes, then I see the complete Description.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange.

Would you mind attaching the script you are using ?

I can't seem to be able to reproduce the problem


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

0 Kudos
nl26348
Contributor
Contributor
Jump to solution

@"
===============================================================================
Title:         Export-VMInfoAOSI.ps1
Description:   Exports VM Information from vCenter into a .CSV file for importing into anything
Usage:         .\Export-VMInfoAOSI.ps1
Date:          31/01/2012
Author        
CSV location  
===============================================================================
"@
filter Get-FolderPath {
    $_ | Get-View | % {
        $row = "" | select Name, Path
        $row.Name = $_.Name
        $current = Get-View $_.Parent
       $path = $_.Name # Uncomment out this line if you do want the VM Name to appear at the end of the path
        $path = ""
        do {
            $parent = $current
            if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
            $current = Get-View $current.Parent
        } while ($current.Parent -ne $null)
        $row.Path = $path
        $row
    }
}
$VCServerName = "xxxxx"
$VC = Connect-VIServer $VCServerName
$VMFolder = "*"
$ExportFilePath = Get-Date -Uformat "\\hostname\vminfo$\%Y%m%d-Export-VMInfo-NL-SI-test.csv"
$Report = @()
$VMs = Get-Folder $VMFolder | Get-VM
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
      $VMView = $VM | Get-View
      $VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,Notes,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed
      $VMInfo.VMName = $vm.name
      $VMInfo.Powerstate = $vm.Powerstate
      $VMInfo.OS = $vm.Guest.OSFullName
      $VMInfo.Folder = ($vm | Get-Folderpath).Path
      $VMInfo.IPAddress = $vm.Guest.IPAddress[0]
      $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Notes = $vm.Notes
      $VMInfo.Host = $vm.host.name
      $VMInfo.Cluster = $vm.host.Parent.Name
      $VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
      $VMInfo.NumCPU = $vm.NumCPU
      $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
      $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
      $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
      $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
      $Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
# $VC = Disconnect-VIServer -Confirm:$False

I see the second part of the notes text after the semicolon in column B in the exportinfo csv file.

Where I expected in column A

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's most probably because the semicolon is the CSV separator in your regional settings.

Try using the Delimiter parameter on the Export-Csv cmdlet, that way you can assign an other character as a CSV delimiter.

Just to confirm that the semicolon is causing the problem.


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

0 Kudos
nl26348
Contributor
Contributor
Jump to solution

Yes, you're right.

Export-Csv $ExportFilePath -Delimiter ";" -NoTypeInformation  fixed my problem.

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only problem you will now have is that Excel will not be able to display the columns correctly, since it expects the default separator.


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

0 Kudos