suboss87
Enthusiast
Enthusiast

Powercli script to get datacenter and it's clusters, hosts ands vm's in a CSV file.

Jump to solution

HI -

      I need a help to write a powercli-script which collect vcenter informations such as list of each Datacenter and it's Clusters, hosts and vm's in a CSV file.

Thanks in advance!!!

-Subash.

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can do something like this.

But be aware that this will not report VMHost, Clusters and Datacenters that do not have any VM in them.

foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

            Get-VM -Location $esx |

            Select @{N='Datacenter';E={$dc.Name}},

                @{N='Cluster';E={$cluster.Name}},

                @{N='VMhost';E={$esx.Name}},Name

        }

    }

}


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

View solution in original post

0 Kudos
32 Replies
LucD
Leadership
Leadership

You can do something like this.

But be aware that this will not report VMHost, Clusters and Datacenters that do not have any VM in them.

foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

            Get-VM -Location $esx |

            Select @{N='Datacenter';E={$dc.Name}},

                @{N='Cluster';E={$cluster.Name}},

                @{N='VMhost';E={$esx.Name}},Name

        }

    }

}


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

0 Kudos
suboss87
Enthusiast
Enthusiast

HI LucD,

               Thanks for your prompt response, You always rock Smiley Happy

Let me try the script and let you know the output!

-Subash.

0 Kudos
suboss87
Enthusiast
Enthusiast

Hi LuCD,

               The Script is working like a charm, However i have two concerns to complete my requirement.

1. VM information is missing to generate along with the output (datacenter, cluster, hosts)

2. Can't able to save in a CSV file

Need guidance on the above areas, Thanks in advance!

-Subash.

0 Kudos
LucD
Leadership
Leadership

1. Which VM information do you want to include in the report, beside the VM name ?

2. A ForEach loop doesn't place anything in the pipeline, but you can fix that by using a call (&) operator.

Like this

&{foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

            Get-VM -Location $esx |

            Select @{N='Datacenter';E={$dc.Name}},

                @{N='Cluster';E={$cluster.Name}},

                @{N='VMhost';E={$esx.Name}},Name

        }

    }

}} | Export-Csv report.csv -NoTypeInformation -UseCulture

 


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

suboss87
Enthusiast
Enthusiast

1. The vm name whichever reside on hosts - may be like this

DatacenterClsuterHostsVM
DC1Cluster-1host1vm1, vm2, vm3...
Clsuter-2host2vm4,vm5,vm6...

2. I will do test and give you an update.

Thanks Smiley Happy

-Subash

0 Kudos
LucD
Leadership
Leadership

Try this variation

&{foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        Get-VMHost -Location $cluster |

        Select @{N='Datacenter';E={$dc.Name}},

            @{N='Cluster';E={$cluster.Name}},

            Name,

            @{N='VM';E={[string]::Join(',',(Get-VM -Location $_ | Select -ExpandProperty Name))}}

    }

}} | Export-Csv report.csv -NoTypeInformation -UseCulture


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

suboss87
Enthusiast
Enthusiast

Hello LuCD,

                      Indeed! I  appriciate you efforts, what i was exactly expected was done finally - The script is really amazing!!!

Million Thanks for your valuble support!

-Subash.

0 Kudos
DaddyDee
Enthusiast
Enthusiast

Hi LucD,

In addition to your script, I'm trying to incorporate the following properties:

  • VM O/S
  • VM vCPU
  • VM RAM (GB)
  • VM Power State
  • VM Folder

Bonus (not a must!!)

  • ESXi Model
  • VM Guest Count on each Physical Host

As always, you help and support is kindly appreciated.

0 Kudos
LucD
Leadership
Leadership

That would mean the report layout has to change.
Instead of 1 ESXi per line, you will then have 1 VM per line.

But that would also mean you will have a lot of repetitive info (for the ESXi node) on each VM line.


Or do you have another layout in mind?
Perhaps separate CSV or worksheets?


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

0 Kudos
DaddyDee
Enthusiast
Enthusiast

The requirement is to list VMs per Datacenter, Cluster, ESXi host and then provide the following:

VM O/S

VM vCPU

VM RAM (GB)

VM Power State

VM Folder

Having knocked up the below script, the cluster property does not appear in final output:

Get-Cluster | Get-VM | Select-Object Name, PowerState, NumCpu, MemoryGB, Guest, Folder |

Export-Csv -Path "c:\Scripts\VM_Config-1.csv" -NoTypeInformation -UseCulture

----------------------------------------------------------------------------------------

PS.

I'm unable to tie in cluster with Datacenter property using the above example?

0 Kudos
LucD
Leadership
Leadership

The pipeline variable helps in that case.

Get-Datacenter -PipelineVariable dc |

Get-Cluster -PipelineVariable cl -ErrorAction SilentlyContinue |

Get-VM -ErrorAction SilentlyContinue |

Select-Object @{N='Datacenter';E={$dc.Name}},

    @{N='Cluster';E={$cl.Name}},

    Name, PowerState, NumCpu, MemoryGB, Guest, Folder


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

DaddyDee
Enthusiast
Enthusiast

Cheers for that LucD,

For some strange reason, the script only outputted info against one DC in one of the two VCs cf which numerous DCs, Clusters, VMs are held.

add-pssnapin VMware.VimAutomation.Core

$Credential = Get-Credential

Connect-VIServer -Server VC1,VC2 -Credential $Credential

Get-Datacenter -PipelineVariable dc |

Get-Cluster -PipelineVariable cl -ErrorAction SilentlyContinue |

Get-VM -ErrorAction SilentlyContinue |

Select-Object @{N='Datacenter';E={$dc.Name}},

    @{N='Cluster';E={$cl.Name}},

    Name, PowerState, NumCpu, MemoryGB, Guest, Folder |

Export-Csv -Path "c:\Scripts\Cluster_VMs.csv" -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

I suspect that would require another forach loop (on the outside of the current ones), that loops through all the vCenters.

Are you connected to all vCenters (check $global:defaultviservers), or are you running in linked-mode?


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

0 Kudos
DaddyDee
Enthusiast
Enthusiast

DefaultVIServerMode is set to Multiple. 

Can you kindly do the honours my friend?  😉

0 Kudos
DaddyDee
Enthusiast
Enthusiast

Having added the forach loop, this didn't seem to make make any difference.

Having gone through script, piece by piece, it would appear issue centered around Datacenter property!!  To get around this, I stripped out the aforementioned references and was then able to get output across vCenter Server instances, minus named property.

In any case, thanks for your input as always.

0 Kudos
lisalorber
Contributor
Contributor

This great! Is there a way to add the IP address of the vm and last powered on date?

0 Kudos
LucD
Leadership
Leadership

Try like this.

But be aware that the presence of the IP address depends on VMware Tools running.

Also note that the retrieval of events might be the script considerably slower.

foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

            Get-VM -Location $esx |

            Select @{N='Datacenter';E={$dc.Name}},

                @{N='Cluster';E={$cluster.Name}},

                @{N='VMhost';E={$esx.Name}},Name,

                @{N='IP';E={$_.Guest.IPAddress -join '|'}},

                @{N='Last PowerOn';E={

                    Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) |

                    where{$_ -is [VMware.Vim.VmPoweredOnEvent]} |

                    Sort-Object -Property CreatedTime -Descending |

                    Select -First 1 -ExpandProperty CreatedTime

                }}

        }

    }

}


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

0 Kudos
lisalorber
Contributor
Contributor

Thanks LucD. This is great but I realized since we clear the log retention is 30 the powered on will not work for this purpose. Can the powerstate be added and then exported to a CVS?

Thanks so much!

0 Kudos
LucD
Leadership
Leadership

Sure, try like this

$report = foreach($dc in Get-Datacenter){

    foreach($cluster in Get-Cluster -Location $dc){

        foreach($esx in Get-VMHost -Location $cluster){

            Get-VM -Location $esx |

            Select @{N='Datacenter';E={$dc.Name}},

                @{N='Cluster';E={$cluster.Name}},

                @{N='VMhost';E={$esx.Name}},Name,PowerState,

                @{N='IP';E={$_.Guest.IPAddress -join '|'}}

        }

    }

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos