kwharrisit
Contributor
Contributor

quick environment report

Jump to solution

has anyone written a script that will list VM's according to Datacenter, Folder, Cluster and host and export to a CSV? Im looking for something to modify and sent out to a couple of managers that keep up with that data for capacity purposes.Any help is appreciated.

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership

Hi Jana,

Afaik it is not possible to find the template from which a VM is deployed. It is not in the properties of the virtual machine and it is also not in the eventlog. That it is not in the eventlog looks like a bug in vSphere 4.1. The FullFormattedMessage of the deploy event say something like:

FullFormattedMessage : Deploying VM1 on host ESX1.domain.country in DATACENTER1 from template VM1

While it should say:

FullFormattedMessage : Deploying VM1 on host ESX1.domain.country in DATACENTER1 from template TEMPLATE1

If this was correct we could get the template from the eventlog with a technique like the one used in Who created that VM ?.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
12 Replies
RvdNieuwendijk
Leadership
Leadership

The next PowerCLI script will give you a .csv file VMs.csv with a list of virtual machines and their Datacenter, Folder, Cluster and host:

Get-VM | ForEach-Object {
  $Report = "" | Select-Object -Property VM,Datacenter,Folder,Cluster,Host
  $Report.VM = $_.Name
  $Report.Datacenter = ($_ | Get-Datacenter).Name
  $Report.Folder = $_.Folder
  $Report.Cluster = ($_ | Get-Cluster).Name
  $Report.Host = $_.VMHost
  $Report
} | Export-Csv -NoTypeInformation -Path VMs.csv

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
kwharrisit
Contributor
Contributor

this looks great..... one more question.... is there a way to denote a vm from a template in this report?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

The script in my first post only returns virtual machines. If you want templates also, I will have to modify the script.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
kwharrisit
Contributor
Contributor

since the managers really want to know what templates are also in use, having that data would be great... Thank you so much.

0 Kudos
janardhanr
Enthusiast
Enthusiast

WoW, I liked this, Could you also publish us with the script which can get the template from which a VM is deployed that will really help!!!

Thanx

Regards,

Jana.

NOTE: If your problem or questions has been resolved / answered, please mark this thread as answered and award points accordingly.

Regards, Jana. NOTE: If your problem or questions has been resolved / answered, please mark this thread as answered and award points accordingly.
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The template object has different properties than the virtual machine object in PowerCLI. I still don't understand why. So to find Datacenter, Host and Cluster for a template goes different than for a virtual machine. Here is the script that returns the information for both virtual machines and templates. To see the difference I added a column Type that has the value VM or Template:

$Report = @()
Get-VM | ForEach-Object {
  $Row = "" | Select-Object -Property Name,Type,Datacenter,Folder,Cluster,Host
  $Row.Name = $_.Name
  $Row.Type = "VM"
  $Row.Datacenter = ($_ | Get-Datacenter).Name
  $Row.Folder = $_.Folder
  $Row.Cluster = ($_ | Get-Cluster).Name
  $Row.Host = $_.VMHost
  $Report += $Row
}
Get-Template | ForEach-Object {
  $Row = "" | Select-Object -Property Name,Type,Datacenter,Folder,Cluster,Host
  $VMHost = Get-VIObjectByVIView -ViView (Get-View -Id $_.ExtensionData.Runtime.Host)
  $Row.Name = $_.Name
  $Row.Type = "Template"
  $Row.Datacenter = Get-Datacenter -VMHost $VMHost
  $Row.Folder = (Get-View -Id $_.FolderId).Name
  $Row.Cluster = (Get-Cluster -VMHost $VMHost).Name
  $Row.Host = $VMHost.Name
  $Report += $Row
}
$Report | Export-Csv -NoTypeInformation -Path VMs.csv

The script uses PowerCLI v4.1 features. So be sure to use that version.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Hi Jana,

Afaik it is not possible to find the template from which a VM is deployed. It is not in the properties of the virtual machine and it is also not in the eventlog. That it is not in the eventlog looks like a bug in vSphere 4.1. The FullFormattedMessage of the deploy event say something like:

FullFormattedMessage : Deploying VM1 on host ESX1.domain.country in DATACENTER1 from template VM1

While it should say:

FullFormattedMessage : Deploying VM1 on host ESX1.domain.country in DATACENTER1 from template TEMPLATE1

If this was correct we could get the template from the eventlog with a technique like the one used in Who created that VM ?.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
kwharrisit
Contributor
Contributor

Robert,

The report is working nicely for the most part, but for some reason the host column is completely blank. this is being run against the vsphere 4.1 environment .Any ideas wha might be going on?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

I'm using vSphere 4.1 also so that can't be the problem. It looks like you are not using PowerCLI 4.1. You can check the version with the Get-PowerCLIVersion cmdlet. It should return:

[vSphere PowerCLI] C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-PowerCLIVersion

PowerCLI Version
----------------
VMware vSphere PowerCLI 4.1 build 264274

You can download VMware vSphere PowerCLI 4.1 from http://www.vmware.com/go/powercli.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
kwharrisit
Contributor
Contributor

Thats exactly what was wrong.... I actually figured that out late last night before your email.. Smiley Happy

Report works nicely. The only thing Im trying to figure out now is how to add another 2 columns that show allocated and actual space usage since these are being thin provisioned.

Thank you so much for the first part though!

0 Kudos
RvdNieuwendijk
Leadership
Leadership

I have added the UsedSpaceGB and ProvisionedSpaceGB columns to the script:

$Report = @()
Get-VM | ForEach-Object {
  $Row = "" | Select-Object -Property Name,Type,Datacenter,Folder,Cluster,Host,UsedSpaceGB,ProvisionedSpaceGB
  $Row.Name = $_.Name
  $Row.Type = "VM"
  $Row.Datacenter = ($_ | Get-Datacenter).Name
  $Row.Folder = $_.Folder
  $Row.Cluster = ($_ | Get-Cluster).Name
  $Row.Host = $_.VMHost
  $Row.UsedSpaceGB = $_.UsedSpaceGB
  $Row.ProvisionedSpaceGB = $_.ProvisionedSpaceGB
  $Report += $Row
}
Get-Template | ForEach-Object {
  $Row = "" | Select-Object -Property Name,Type,Datacenter,Folder,Cluster,Host,UsedSpaceGB,ProvisionedSpaceGB
  $VMHost = Get-VIObjectByVIView -ViView (Get-View -Id $_.ExtensionData.Runtime.Host)
  $Row.Name = $_.Name
  $Row.Type = "Template"
  $Row.Datacenter = Get-Datacenter -VMHost $VMHost
  $Row.Folder = (Get-View -Id $_.FolderId).Name
  $Row.Cluster = (Get-Cluster -VMHost $VMHost).Name
  $Row.Host = $VMHost.Name
  $Committed = 0
  $Uncommitted = 0
  $PerDatastoreUsage = $_.ExtensionData.Storage.PerDatastoreUsage
  $PerDatastoreUsage | Foreach-Object {
    $Committed += $_.Committed
    $Uncommitted += $_.Uncommitted
  }
  $Row.UsedSpaceGB = $Committed / 1GB
  $Row.ProvisionedSpaceGB = ($Committed + $Uncommitted) / 1GB
  $Report += $Row
}
$Report | Export-Csv -NoTypeInformation -Path VMs.csv

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
AlbertWT
Virtuoso
Virtuoso

Thanks man,

this script is so great !

Kind Regards,

AWT

/* Any kind of comment or input would be greatly appreciated */
0 Kudos