VMware Cloud Community
WJPConway
Contributor
Contributor
Jump to solution

Change the Report_VirtualMachine.ps1 to only get information on VMs in specified Folders

I have been using LucDs script Report_VirtualMachine per the linked communities discussion (VMware PowerCLI Forum - VMware {code} )

The script is extremely useful - but I am now dealing with a vCenter that has over 7000 vm's - its linked to a vCloud Director instance and as vCloud Director organizes VMs in folders the ideal for me would be to limit the script to a number for folders.

Ideally I would have something like

$folders = 'vAPP1, vAPP2, vAPP3'

The report would then show only vms in the 3 folders vAPP1 - 3. It would also create an extra field called folder so the report would show which folder each vm is in.

I have tried changing the below section - to get started with it getting the same for 1 folder (with step 2 being multiple folders. But alas I am not from a scripting background and my reports end up empty.

I don't have the knowledge to write my own script so my bets bet is altering an existing one to get the info I need. Does anyone know how I could alter this sript so it would limit the report to certain folders - I think this would be extremely useful for anyone using vCD or any folder orientated deployment tool

$report = @()

foreach($vm in Get-Folder 'vAPP1' -NoRecursion | Get-VM){
    $vms = "" | Select-Object VMName, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUreservation, TotalMemory, MemoryUsage, MemoryReservation, TotalNics, ToolsStatus, ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore, Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, NB_LAST_BACKUP, Owner
    $vms.VMName = $vm.Name
    $vms.VMState = $vm.summary.runtime.powerState
    $vms.Boottime = $vm.Runtime.BootTime
    $vms.TotalNics = $vm.summary.config.numEthernetCards
    $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name
    $vms.OS = $vm.Config.GuestFullName
    $vms.Hostname = $vm.guest.hostname
    $vms.IPAddress = $vm.guest.ipAddress
    $vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandP

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think it is easier to have an additional loop over all the folders.
You can add additional foldernames on the Name parameter.
Also note that the script uses a vSphere object, hence the Get-View on the inner loop.

Try like this

$report = @()

foreach ($folder in Get-Folder -Name 'vAPP1')

{

   foreach ($vm in (Get-View -ViewType VirtualMachine -SearchRoot $folder.ExtensionData.MoRef))

   {

   $vms = "" | Select-Object VMName, Folder, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUreservation, TotalMemory, MemoryUsage, MemoryReservation, TotalNics, ToolsStatus, ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore, Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, NB_LAST_BACKUP, Owner

   $vms.VMName = $vm.Name

   $vms.Folder = $folder.Name

   $vms.VMState = $vm.summary.runtime.powerState

   $vms.Boottime = $vm.Runtime.BootTime

   $vms.TotalNics = $vm.summary.config.numEthernetCards

   $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name

   $vms.OS = $vm.Config.GuestFullName

   $vms.Hostname = $vm.guest.hostname

   $vms.IPAddress = $vm.guest.ipAddress

   $vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandP

   $report += $vms

   }

}

$report


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

I think it is easier to have an additional loop over all the folders.
You can add additional foldernames on the Name parameter.
Also note that the script uses a vSphere object, hence the Get-View on the inner loop.

Try like this

$report = @()

foreach ($folder in Get-Folder -Name 'vAPP1')

{

   foreach ($vm in (Get-View -ViewType VirtualMachine -SearchRoot $folder.ExtensionData.MoRef))

   {

   $vms = "" | Select-Object VMName, Folder, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUreservation, TotalMemory, MemoryUsage, MemoryReservation, TotalNics, ToolsStatus, ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore, Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, NB_LAST_BACKUP, Owner

   $vms.VMName = $vm.Name

   $vms.Folder = $folder.Name

   $vms.VMState = $vm.summary.runtime.powerState

   $vms.Boottime = $vm.Runtime.BootTime

   $vms.TotalNics = $vm.summary.config.numEthernetCards

   $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name

   $vms.OS = $vm.Config.GuestFullName

   $vms.Hostname = $vm.guest.hostname

   $vms.IPAddress = $vm.guest.ipAddress

   $vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandP

   $report += $vms

   }

}

$report


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

WJPConway
Contributor
Contributor
Jump to solution

You make it look so easy - thanks very much Luc - worked perfectly.

0 Kudos