VMware Cloud Community
dmaster
VMware Employee
VMware Employee
Jump to solution

Create a custom report about virtual machines in virtual center with the VI Toolkit

Hello All,

I am new to the VI Toolkit and try to get some help for the issue below ..

I need to create a list off all my vm's in virtualcenter 2.5 with the vm name, memory limit and memory reservation + filter out the memory limit ones with a value of -1 (if possible save this to text file like "report.csv")

How can you do this with the VI Toolkit?

I managed to get output for only one of the rows, who can help me out ?

+++++++++++++++++++++++++++++++++++++++++++++++++++++

ouput should look like

virtualmachine name;memory limit; memory reservation

vm1;2048;1024

vm22;1024;512

vm41;4096;2048

+++++++++++++++++++++++++++++++++++++++++++++++++++++

The code:

$vmall = Get-VM *

#Create a variable that contains all VM's names

$vmview = $vmall | foreach-object { get-view $_.id }

#Create a variable that contains the exposed configurable options for each of the VM's contained in the $vm variable

$vmview | foreach-object { $_.resourceconfig.memoryallocation.limit }

#Display the contents of one of the collums.

output is now like

2048

1024

4096

tried things like, but i can't get it working yet..

$vmview | foreach-object { $_.config.name $_.resourceconfig.memoryallocation.limit $_.resourceconfig.memoryallocation.reservation}

and how to include saving output to a file and do the filtering part stays also a riddle ..

Who can help me out ?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Are you using the latest version, the one called VMreport-nofilter.ps1 without the memory limit test ?

The last line I sent ($report | out-default) doesn't seem to show the entries.

Instead replace that line by the following

foreach($entry in $Report){
  $entry | Out-Default
}

That should give you some output at least.


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

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this should do the trick


$Report = @()
 
get-vm | % {
  $vm = Get-View $_.ID
  if($vm.resourceconfig.memoryallocation.limit -ne -1){
    $ReportRow = "" | Select-Object VMName, MemoryLimit, MemoryReservation
    $ReportRow.VMName = $vm.Name
    $ReportRow.MemoryLimit = $vm.resourceconfig.memoryallocation.limit
    $ReportRow.MemoryReservation = $vm.resourceconfig.memoryallocation.reservation
    $Report += $ReportRow
  }
} 
$Report | Export-CSV <filename.csv>

All the Powershell features used in the script were already discussed in this community but if something is unclear don't hesitate to ask.


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

dmaster
VMware Employee
VMware Employee
Jump to solution

Hello LucD,

Thanx for helping me..

I tried your code... looks impressive Smiley Wink

(i am trying to understand the syntax..)

$Report = @() get-vm | % { $vm = Get-View $_.ID if($vm.resourceconfig.memoryallocation.limit -ne -1){ $ReportRow = "" | Select-Object VMName, MemoryLimit, MemoryReservation $ReportRow.VMName = $vm.Name $ReportRow.MemoryLimit = $vm.resourceconfig.memoryallocation.limit $ReportRow.MemoryReservation = $vm.resourceconfig.memoryallocation.reservation $Report += $ReportRow } } $Report | Export-CSV test.csv

when i try it, it directly generates an error message..

Unexpected token 'get-vm' in expression or statement.

At line:1 char:21

+ $Report = @() get-vm &lt;&lt;&lt;&lt; | % { $vm = Get-View $_.ID if($vm.resourceconfig.m

emoryallocation.limit -ne -1){ $ReportRow = "" | Select-Object VMName, MemoryLi

mit, MemoryReservation $ReportRow.VMName = $vm.Name $ReportRow.MemoryLimit = $v

m.resourceconfig.memoryallocation.limit $ReportRow.MemoryReservation = $vm.reso

urceconfig.memoryallocation.reservation $Report += $ReportRow } } $Report | Exp

ort-CSV test.csv

do i miss something ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks like the linefeeds and carriage returns are missing.

Try opening your script in notepad or any other editor.

What browser are you using the look at the community ?

With Firefox I get the best results.

And of course first connect to the VC server with

Get-VIServer -Server <VC-server> -User <username> -Password <password>


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

Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

I made some typo's in my last reply.. so please review it..

there is nothing wrong with the linefeeds and carriage returns. everything i typed in is just like your example.

looks there is an syntax error in the script (line 1 character 21 that is just after get-vm)? do you have any idea ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could you try pasting the script in a file (with the extension .ps1) and then execute it from the VI Toolkit for Windows prompt.

You can do that with the command

<pre>
.<script>.ps1
</pre>

It is possible you first have to allow the execution of Powershell scripts with

<pre>
set-executionpolicy –ExecutionPolicy “RemoteSigned”
</pre>

For your convenience I have attached the script


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

Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello LucD,

i made some progress, your script is running now. i used the PowerUI Script Editor to check the code.

it is running when i put the script in een .ps1 file and run this file.

The only drawback, the .csv file is empty ?

I think we are almost there.. but i still need some extra help.

note1: i changed from internet explorer to mozilla firefox now i see what you mean with the missing linefeeds and carriage returns..

note2: when is run the following:

&lt;pre&gt;

set-executionpolicy –ExecutionPolicy “RemoteSigned”

&lt;/pre&gt;

i get the following error

Parsing error:

At line 2, position 22

Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.

any idea ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try the attached script, I removed the test.

This should give you all your VMs.

The forum SW changed the Set-ExecutionPolicy cmdlet.

It should say

Set-ExecutionPolicy -executionPolicy RemoteSigned

But since you script is running it looks to be OK.

You can always use the Get-Help cmdlet to get info about a cmdlet.

Get-Help Set-ExecutionPolicy


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

Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello LucD,

Thanx for your reply.

Set-ExecutionPolicy cmdlet is working now.

But the output of the .csv file is still empty.

I also tried some extra parameters for the export part.

$Report | Export-CSV c:\temp\VMreport.csv -Encoding UTF8

or

$Report | Export-CSV c:\temp\VMreport.csv -Encoding Unicode

But both results in an empty .csv file ?

Maybe we can first try to get the output to the screen ? To see if the rest of the script is finding what it should find.

How should i manage this ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Hi dmaster,

You can replace the Export-CSV cmdlet with the Out-Default cmdlet.

This should send the output to screen.

$Report | Out-Default


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

Reply
0 Kudos
rdiphoorn
Enthusiast
Enthusiast
Jump to solution

dmaster,

You have to use quotation marks:

$Report | Export-CSV c:\temp\VMreport.csv -Encoding "UTF8"

This extra option removes the .NET object type in the first line of your excel document:

$Report | Export-CSV c:\temp\VMreport.csv -Encoding "UTF8" -notype

Clarification is a beautiful thing. Check my site: http://www.vituality.com
Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello LucD,

There must be something wrong with the report script.

Also when i try to get the output on my screen, it's empty.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you using the latest version, the one called VMreport-nofilter.ps1 without the memory limit test ?

The last line I sent ($report | out-default) doesn't seem to show the entries.

Instead replace that line by the following

foreach($entry in $Report){
  $entry | Out-Default
}

That should give you some output at least.


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

Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello LucD,

I managed to get some output out your script..

The strange thing is when i run the script the first time it works great, it works for a certain time. Finally i get no proper results back and i have to restart the VI Toolkit.

Any idea, do you see the same behaviour ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Glad to hear it showed at least some of the info you wanted.

I have to admit that I don't have a lot of experience with PowerGui, I use PowerShell+ most of the time.

Great debugger Smiley Wink

Do you also see the behavior when you run the script from the VMware VI Toolkit (for Windows) prompt more then once ?

I never experienced the behavior you described.


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

Reply
0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

We found the reason of the strange behaviour, was also mentioned in another topic on the forums.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

We have confirmed that this is a bug. In an earlier version of the

toolkit there were 2 ways to authenticate, get-viserver and

connect-vim. Connect-vim was used for the get-view and find-entityview

cmdlets. We merged these for the Beta.

Unfortunately, if you connect using get-viserver and your session times

out, get-viserver won't refresh the timed-out connection that is used

for the get-view and find-entityview cmdlets.

The workaround is to close the session and start a new one. We will have a fix for this in our GA release.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Reply
0 Kudos