VMware Cloud Community
TestADE
Contributor
Contributor
Jump to solution

VM's number by pool

Hello,

I did some investigations but i did not find a good answer to my problem (so i apologize if there is already a topic with the answer).

In the infrastructure we have many differents pools with few hundreds of VMs inside and I have to write a script that count the number of used VM and unused VM on each pool and export it inside a CSV.

I started using this command :

Get-ResourcePool | Select Name, @{N="VM#";E={$_.ExtensionData.Vm.Count}}

but i don't know to see unused VM and export the result in a good-looking CSV file.

I would appreciate some tips if someone can help me.

Best regards,

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, use the NoClobber switch

Get-ResourcePool |

Select Name,

    @{N='PoweredonVM';E={

        $script:totalVM = Get-View -Id $_.ExtensionData.VM -Property Runtime.PowerState

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredoff'}).Count}},

    @{N='PoweredOffVM';E={

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredon'}).Count}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture -NoClobber


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

View solution in original post

22 Replies
LucD
Leadership
Leadership
Jump to solution

How do you define an "unused" VM?

Is that a VM that is powered off? Or do you use another criterium?


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

Hello LucD,

For me an "unused" VM is a VM that is powered off.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-ResourcePool |

Select Name,

    @{N='PoweredonVM';E={

        $script:totalVM = Get-View -Id $_.ExtensionData.VM -Property Runtime.PowerState

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredoff'}).Count}},

    @{N='PoweredOffVM';E={

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredon'}).Count}}


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

TestADE
Contributor
Contributor
Jump to solution

Thanks for this answer, it works fine. The aim is to export the result in a CSV file and store it each day. I Know how to export it but do you know if it is possible to do this without overwrite the same csv file ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, use the NoClobber switch

Get-ResourcePool |

Select Name,

    @{N='PoweredonVM';E={

        $script:totalVM = Get-View -Id $_.ExtensionData.VM -Property Runtime.PowerState

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredoff'}).Count}},

    @{N='PoweredOffVM';E={

        $script:totalVM.Count - ($script:totalVM | where{$_.Runtime.PowerState -eq 'poweredon'}).Count}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture -NoClobber


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

TestADE
Contributor
Contributor
Jump to solution

I'm sure this should work fine but i changed the path of the .csv to something like "c:..." and i got an error because the name of the file is already existing. Is there a possibility to add the date/hour at the end of the CSV file in order to avoid this error ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change that last line to

Export-Csv -Path "C:\Reports\report-$(Get-Date -Format yyyyMMdd-HHmm).csv" -NoTypeInformation -UseCulture -NoClobber


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

TestADE
Contributor
Contributor
Jump to solution

Thanks a lot LucD, i learnt so much from that and it works very well.

Have a nice day.

Regards,

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

Hello again,

​First of all thank you for answering me this morning LucD, your script really helped me. ​

​Now i'm having an other question because this morning i was confused me and i mixed up Pool in vSphere and pool in View (HorizonView). Is it possible to do the same thing than i asked you this morning but in View and not in vSphere ?

​I tried to do it but it wasn't successful.

​I would appreciate your help (again)

​Best regards,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which Horizon version are you using?

And also which PowerCLI version?


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

In few days we will upgrade from HorizonView 5.3 to 7.6.

And about the PowerCLI version : VMware vSphere PowerCLI 5.8 Release 1 build 2057893

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you upgrade to Horizon 7.* you will not have to execute the View cmdlets on the Connection Server anymore.

In the recent PowerCLI installations (currently 10.2.0) there is a Horizon module included, but it will only work for Horizon 6.* and higher.

Do you want a solution for the remaining View 5.* days, or do you want to wait till you get to Horizon 7.*?

While you are waiting, you can already prepare a station for your PowerCLI scripts.
First make sure you have a recent PowerShell version. I suggest 5.1.
Then install the latest PowerCLI version from the PSGallery.

See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates

You might also want to install the HV.Helper module.

See Automating VMware Horizon 7 with VMware PowerCLI 6.5

Don't mind the PowerCLI 6.5 reference in there, the post is equally valid for PowerCLI 10.2.0


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

Ok because i thought that if my script is working on Horizon 7.* he will still be working on 5.* the same way.

I don't know yet if our PowerCLI will be updated during the update of Horizon or if it is a different thing (I am new with those technologies..). If we kept the old one (5.8) the only impact will be the impossibility to use the Horizon module ?!

About the solution, do you think it is possible doing a script for Horizon 7.* which is still working too on Horizon 5.* or it is better focus on a version ?

I will prepare the station with your advices.

Thanks a lot LucD.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With Horizon 7 you can still install the PSSnapin on the Connection Servers (if I'm not mistaken).
During the View to Horizon upgrade you can decide to also install the old PSSnapin on the Connection Servers.
So if you keep using the 5.* code, that should also work against Horizon 7.*.

BUT, you'll be forced to run your scripts on the Connection Server (bad practice) and the PSSnapin will not be maintained anymore.

My advise, prepare a workstation with the latest PowerCLI version, and then connect to the Connection Server from that workstation.


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

Thanks for those informations, i will keep that in mind.

This kind of decision could take a long time depending on my direction :smileysilly:. So i think i will try to do a script with current (5.*) versions and upgrade/modify it later. Do you have any advices on the way to do it in this version ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I haven't used the old View PSSnapin in a while.

But perhaps this old post from ALan might help?
See GATHERING SIMPLE POOL INFORMATION FROM VMWARE VIEW

I remember that Get-DesktopVM returns the VMs as strings, but once you have to those you can do a regular Get-VM, check the PowerState and do the count.


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

I have already tried it but maybe in a wrong way, i will try again today and i will come back here if needed.

Thank you

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Make sure to include the code you are using, so I can have a look.


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

0 Kudos
TestADE
Contributor
Contributor
Jump to solution

After some discussions with technicians, they told me that the server i am working on don't have a VIEW PowerCLI, there is only a PowerCLI vSphere. In order to by-pass this they used this methodMonitoring View with Nagios (uadv_vmware_view.psm1) with Powershell which is using Unofficial VMware View PowerShell cmdlets (http://myvirtualcloud.net/unofficial-advanced-vmware-view-powershell-cmdlets/)

Here is the current process using Powershell :

- Import-module "...\uadv_vmware_view.psm1"

- Connect-ViewConnServer "..." "..." "... "..."

Then a successful message appeared : "Connected to the VMware View Connection server ..."

I realized that i am not having a lot of flexibility this time and i will be forced to use those unofficial commands but i have absolutely no idea on how to proceed because i don't know how to extract information from "get-pools" and count the number of PoweredOn and PoweredOff VMs..Smiley Sad

Can you help me (one more time..) exporting on a csv file a list with our pools and the number of poweredOn and poweredOff VMs for each pools ?

0 Kudos