Posted in PowerCLI Script VMware

Make a Report of your Virtual Machines

Make a Report of your Virtual Machines Posted on 15/06/2018
Description :

This script filter your VMs by state, and VM name as well. You can see all of these informations in the report (Name, Folder, Datastore, VMPath, State) as well as export to CSV file if you add Export-CSV option.

Syntax :

MyVMinformation [-vmState] {PoweredOn | PoweredOff} [-vmName] []

Examples :
PowerCLI C:\> MyVMinformation -vmState PoweredOff -vmName server2-2
Name      : server2-2
Folder    : vm
Datastore : FreeNas.5-7k2-vol1
VMPath    : [FreeNas.5-7k2-vol1] server2/server2.vmx
State     : PoweredOff

PowerCLI C:\> MyVMinformation -vmState PoweredOff -vmName *
You can see all your VMs Powered Off

PowerCLI C:\> MyVMinformation -vmState PoweredOn -vmName * | Export-Csv c:\Report.csv
Export all your VMs Powered On in a CSV file Report.csv on C:\
Script :
#Script By Olivier Gosselin - France
#Tools PowerCLI 6.5 Release 1

#$vCenterFQDN = "vCenterFQDN"
#$CredentialvCenter = Get-Credential -Message "Credential vCenter"
#Connect-VIServer $vCenterFQDN -Credential $CredentialvCenter

function MyVMinformation {
    [ValidateSet("PoweredOn", "PoweredOff")]


# ID Datastore
$DatastoreName = Get-Datastore -Name * | select Id,Name

# Filtre les VMs PoweredOn
$VMs = Get-VM -Name "$vmName" | where {$_.PowerState -eq "$vmState"}

foreach ($VM in $VMs) {
$Datastores = $VM.DatastoreIdList
$VMName = $VM.Name
$VMFolder = $VM.Folder
$VMState = $VM.PowerState
$VMPath = $VM.ExtensionData.Config.Files.VmPathName

foreach ($Datastore in $Datastores) {
$Datastore = $DatastoreName | where {$_.Id -eq "$Datastore"}
$Datastore = $Datastore.Name
$Output = New-Object psobject
$Output | Add-Member -MemberType NoteProperty -Name Name -Value (@($VMName) -join ',')
$Output | Add-Member -MemberType NoteProperty -Name Folder -Value $VMFolder
$Output | Add-Member -MemberType NoteProperty -Name Datastore -Value $Datastore
$Output | Add-Member -MemberType NoteProperty -Name VMPath -Value $VMPath
$Output | Add-Member -MemberType NoteProperty -Name State -Value $VMState