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
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
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
Thank you very much, your script works.
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/
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
great!! thanks guru
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?
No clue.
Is the value complete in $vmInfo.Notes ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I've got the same result with $vmInfo.Notes
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
Yes, then I see the complete Description.
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
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
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
Yes, you're right.
Export-Csv $ExportFilePath -Delimiter ";" -NoTypeInformation fixed my problem.
Thanks!
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