VMware Cloud Community
GuruAnt
Contributor
Contributor
Jump to solution

Help with arrays

I'm just starting to work with arrays, and they're not very well covered by the book I'm using. I'm trying to create a weekly report, one component of which needs to be a list of machines which have more than a specific amount of RAM (dependent on the OS).

$intXPRAMCap = 256
$intVistaRAMCap = 512
$intServer2003RamCap = 1024
$strFile = "Machines_over_RAM_Allocation.html"
$arrXPMachines = @()
$arrXPMachinesOverRAM = @()
$arrVistaMachines = @()
$arrVistaMachinesOverRAM = @()
$arrServer2003Machines = @()
$arrServer2003MachinesOverRAM = @()
ForEach ($objVM in $objVMs){
$arrXPMachines = $arrXPMachines + (Get-VMGuest -VM $objVM | Where-Object {($_.OSFullName -like "Microsoft Windows XP Professional*") -and ($objVM.MemoryMB -gt $intXPRAMCap)})
$arrVistaMachines = $arrVistaMachines + (Get-VMGuest -VM $objVM | Where-Object {($_.OSFullName -like "Microsoft Windows Vista*") -and ($objVM.MemoryMB -gt $intVistaRAMCap)})
$arrServer2003Machines = $arrServer2003Machines + (Get-VMGuest -VM $objVM | Where-Object {($_.OSFullName -like "Microsoft Windows Server 2003*") -and ($objVM.MemoryMB -gt $intServer2003RAMCap)})
$arrMachinesOverRAMAllocation = $arrXPMachines + $arrVistaMachines + $arrServer2003Machines
}
$arrMachinesOverRAMAllocation | ConvertTo-HTML -Property VMName,OSFullName -title $strFile > $strOutputLogDir\$date\$strFile

This create a nice HTML page with the machine name and the Operating System, but I'd really like it to show the amount of RAM each machine has. This isn't part of the object returned by Get-VMGuest, and I don't know how to integrate it into the array.

Also, if I run the script in the dot-namespace, I can output $arrMachinesOverRAMAllocation, but it just returns a blank when I use $arrMachinesOverRAMAllocation[0], which I thought would work - is what I have above correct?

0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

You should check out Add-Member. Something like this:

$objVM | Add-Member -Name OSFullName -type noteproperty -value ($objVM | Get-VMGuest).OSFullName
$arrXPMachines += $objVM

would add the guest to the VM object, from there you could make the report with VM Name, memory and guest name pretty easily.

View solution in original post

0 Kudos
2 Replies
admin
Immortal
Immortal
Jump to solution

You should check out Add-Member. Something like this:

$objVM | Add-Member -Name OSFullName -type noteproperty -value ($objVM | Get-VMGuest).OSFullName
$arrXPMachines += $objVM

would add the guest to the VM object, from there you could make the report with VM Name, memory and guest name pretty easily.

0 Kudos
GuruAnt
Contributor
Contributor
Jump to solution

That worked perfectly, thanks very much.

    $intXPRAMCap = 512
    $intVistaRAMCap = 1024
    $intServer2003RamCap = 2048
    $strFile = "Machines_over_RAM_allocation.html"
    $arrXPMachines = @()
    $arrXPMachinesOverRAM = @()
    $arrVistaMachines = @()
    $arrVistaMachinesOverRAM = @()
    $arrServer2003Machines = @()
    $arrServer2003MachinesOverRAM = @()
    ForEach ($objVM in $objVMs){
        $objVM | Add-Member -Name OSFullName -type noteproperty -value ($objVM | Get-VMGuest).OSFullName
        $arrXPMachines = $arrXPMachines + ($objVM | Where-Object {($_.OSFullName -like "Microsoft Windows XP Professional*") -and ($objVM.MemoryMB -gt $intXPRAMCap)})
        $arrVistaMachines = $arrVistaMachines + ($objVM | Where-Object {($_.OSFullName -like "Microsoft Windows Vista*") -and ($objVM.MemoryMB -gt $intVistaRAMCap)})
        $arrServer2003Machines = $arrServer2003Machines + ($objVM | Where-Object {($_.OSFullName -like "Microsoft Windows Server 2003*") -and ($objVM.MemoryMB -gt $intServer2003RAMCap)})
        $arrMachinesOverRAMAllocation = $arrXPMachines + $arrVistaMachines + $arrServer2003Machines
    }
    $arrMachinesOverRAMAllocation | ConvertTo-HTML -Property Name,OSFullName,MemoryMb,Host -title $strFile -body "<H1>Machines over RAM Allocation</H1><BR><b>XP RAM Cap:</b> $intXPRAMCap<br><b>Vista RAM Cap:</b> $intVistaRAMCap<BR><b>Server 2003 RAM Cap:</b> $intServer2003RAMCap<P> </p>" > $strOutputLogDir\$date\$strFile

0 Kudos