VMware Cloud Community
mshorrosh
Contributor
Contributor

Adding the folder name for vms inside my reporting script

I've read over the forums and found the following script that works fine for listing out the vm and the corresponding folder, however i'm trying to add this script into my current script and having trouble.

Folderscript

$output = @() # init array

Get-Folder | ForEach-Object {

$FolderName = $_.Name # $_ is the current object (folder) in the loop

$_ | Get-VM | ForEach-Object {

$process = "" | Select-Object FolderName, VMName # the aforementioned shortcut

$process.FolderName = $FolderName

$process.VMName = $_.Name # $_ is now a VM, not a folder

$output += $process

}

}

Write-Output $output

And heres my script that I would like to add this logic to....

$allvms = @()

$vms = Get-Vm

foreach($vm in $vms){

$vmstat = "" | Select VmName, MemMax, MemAvg, CPUMax, CPUAvg, NetAvg, DiskAvg, TotalDisk

$vmstat.VmName = $vm.name

$statcpu = Get-Stat -Entity ($vm) `

-start (get-date).AddDays(-30) `

-Finish (Get-Date) `

-MaxSamples 10000 `

-stat cpu.usagemhz.average

$statmem = Get-Stat -Entity ($vm) `

-start (get-date).AddDays(-30) `

-Finish (Get-Date) `

-MaxSamples 10000 `

-stat mem.usage.average

$statnet = Get-Stat -Entity ($vm) `

-start (get-date).AddDays(-30) `

-Finish (Get-Date) `

-MaxSamples 10000 `

-stat net.usage.average

$statdisk = Get-Stat -Entity ($vm) `

-start (get-date).AddDays(-30) `

-Finish (Get-Date) `

-MaxSamples 10000 `

-stat disk.usage.average

$stattotaldisk = Get-Harddisk -VM $vm

$cpu = $statcpu | Measure-Object -Property value -Average -Maximum

$mem = $statmem | Measure-Object -Property value -Average -Maximum

$net = $statnet | Measure-Object -Property value -Average

$disk = $statdisk | Measure-Object -Property value -Average

$totaldisk = ($stattotaldisk | Measure-Object -Property CapacityKB -sum).Sum

$vmstat.CPUMax = $cpu.Maximum

$vmstat.CPUAvg = $cpu.Average

$vmstat.MemMax = $mem.Maximum

$vmstat.MemAvg = $mem.Average

$vmstat.NetAvg = $net.Average

$vmstat.DiskAvg = $disk.Average

$vmstat.TotalDisk = $totaldisk

$allvms += $vmstat

}

$allvms | `

select VmName, MemMax, MemAvg, CPUMax, CPUAvg, NetAvg, DiskAvg, TotalDisk | `

Export-Csv "c:\VMs.csv" -noTypeInformation

If anyone can help me figure out how to do this that would be very much appreciated!

Reply
0 Kudos
1 Reply
ctrople
Enthusiast
Enthusiast

Try inserting your code block at the start of your foreach loop and subbing Get-VM with your $vm variable. At that point you could probably strip out quite a bit of code, but get it working first and then streamline if you feel inclined.

======================================

Chyna Trople, VCP

Monitor. Correlate. Act. | vWire.com

======================================

Chyna Trople, VCP Monitor. Correlate. Act. | vWire.com
Reply
0 Kudos