VMware Cloud Community
vibes187
Contributor
Contributor

PowerCli report

Hi all,

I'm new to the Vmworld, so forgive me if i ask some silly questions....

I recently taken over running our virtual environment. Im ok using the GUI but have started to play with Powercli as im in the process of upgrading our 2 vCenter servers to 4.1 on 2K8 R2.

Im looking to produce some reports on each folder level before we do the upgrades.... and i specially want to move our test clutser over. What I initially need is to get the following information in a tabularised spreadsheet /doc.

My setup is as follows

Folder called "Dev Hosts", running two ESXi Hosts in "Dev Hosts Cluster" with about 6 Res Pools, and 60 VM's.

I'm looking for the following.

Folder

Cluster 

Resource Pool

And where the VM's are assigned & Preferably what permissons are defined at the various object levels...

Can this be done quite easiy?     Thanks in advance...

0 Kudos
11 Replies
LucD
Leadership
Leadership

There are no silly questions.

Welcome to the PowerCLI Community.

Producing this kind of reports is one the things that PowerCLI is good at.

Try if this produces what you want

$folderName = "Dev Hosts" 
$report = @() foreach($cluster in Get-Cluster -Location (Get-Folder -Name $folderName)){     foreach($resp in Get-ResourcePool -Location $cluster){         foreach($vm in Get-VM -Location $resp){             foreach($perm in Get-VIPermission -eNTITY $VM){                 $row = "" | Select Folder,Cluster,ResourcePool,VM,Principal,Role                 $row.Folder = $folderName
               
$row.Cluster = $cluster.Name                 $row.ResourcePool = $resp.Name                 $row.VM = $vm.Name                 $row.Principal = $perm.Principal                 $row.Role = $perm.Role                 $report += $row
            }         }     } }
$report

This displays the report on screen but you easily export it to a CSV file by changing that last line in

$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
vibes187
Contributor
Contributor

Thanks for your response LucD.

Do i need to specify the data center too? (Dev DC1)

0 Kudos
LucD
Leadership
Leadership

Do you have multiple datacenters under that folder ?

Do you want to include the datacentername in the report ?


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

0 Kudos
vibes187
Contributor
Contributor

I have two Data centers, and mulitple folders under each DC. Dev hosts folders (one of 6 folders)sits under Dev DC1.

If i can include the DC in the report aswell would be good.

I ran the initial script and got the following..

Unexpected token 'report' in expression or statement.
At C:\Test\dev.ps1:1 char:42
+ $folderName = "Dev Hosts" $report <<<<  = @()
    + CategoryInfo          : ParserError: (report:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

0 Kudos
LucD
Leadership
Leadership

Do you want to report for all folders then ?

I understood from the first post that you targetted that specific folder.

The error you got seems to be due to the copy-paste of the code.

That should be 2 lines

$folderName = "Dev Hosts"

$report = @()


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

0 Kudos
LucD
Leadership
Leadership

This version will produce the report for all datacenters and all folders

$report = @()

foreach($dc in Get-Datacenter){
   
foreach($folder in Get-Folder -Location $dc){
       
foreach($cluster in Get-Cluster -Location $folder){
           
foreach($resp in Get-ResourcePool -Location $cluster){
               
foreach($vm in Get-VM -Location $resp){
                   
foreach($perm in Get-VIPermission -Entity $VM){
                       
$row = "" | Select Datacenter,Folder,Cluster,ResourcePool,VM,Principal,Role
                       
$row.Datacenter = $dc.Name
                       
$row.Folder = $folder.Name
                       
$row.Cluster = $cluster.Name
                       
$row.ResourcePool = $resp.Name
                       
$row.VM = $vm.Name
                       
$row.Principal = $perm.Principal
                       
$row.Role = $perm.Role
                       
$report += $row                    }
                }
            }
        }
    }
}
$report


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

0 Kudos
vibes187
Contributor
Contributor

I got the following error (multiple times). Am i doing something wrong?... 

Get-Cluster : Cannot bind parameter 'Location'. Cannot convert the "" value of
type "System.Management.Automation.PSCustomObject" to type "VMware.VimAutomatio
n.ViCore.Types.V1.Inventory.VIContainer".
At C:\Test\new.ps1:5 char:50
+         foreach($cluster in Get-Cluster -Location <<<<  (Get-Folder -Location
$folder)){
    + CategoryInfo          : InvalidArgument: (:) [Get-Cluster], ParameterBin
   dingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.GetCluster

0 Kudos
avlieshout
VMware Employee
VMware Employee

Haven't tested this code, but shouldn't that line just be:

Foreach ($cluster in Get-Cluster -Location $folder){

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
LucD
Leadership
Leadership

Of course.

And I found another typo.

The script above is corrected, try again.


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

0 Kudos
vibes187
Contributor
Contributor

Many thanks, this works now Smiley Happy

If i want to only run this for a specifc folder or cluster and export to Html what would i need to do?

Reason being.. i run the script and it took over 40 mins, and returned just over 45,000 lines in the CSV report..

0 Kudos
LucD
Leadership
Leadership

One way of doing that is by changing the lines that collection of folders and clusters.

For example like this

foreach($dc in Get-Datacenter){
    foreach($folder in Get-Folder -Name MyFolder -Location $dc){
        foreach($cluster in Get-Cluster -Location  $folder){

or
foreach($dc in Get-Datacenter){
    foreach($folder in Get-Folder -Location $dc){
        foreach($cluster in Get-Cluster -Name MyCluster -Location  $folder){

To create a HTML file you can use the ConvertTo-Html cmdlet instead of the Export-Csv cmdlet.

The last line could be something like this

$report | ConvertTo-Html | Set-Content "C:\report.html"

You can make the HTML more fancy by using CSS.


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

0 Kudos