Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 175326

Arrays in arrays in a report.

$
0
0

I have looked all over and I am having a hard time finding what I need. I am fairly new to complex scripting so I am hoping someone here can help.

 

I am trying to create a report for vms. I don't want to post the whole script here as it is long due to menus and such as there may be some not so technical people running it, and want to make it as easy as possible.

 

I am using the Powershell Module ImportExcel to export it into a spreadsheet, also mutipule csv files if module not installed.

 

So to export to one of the worksheets, I have as follows

 

$VMData2 = @()

        $infoObject2= New-Object PSObject

        #Second Page of Excel / Csv - Guest Info

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Name" -value $VM

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Folder" -value $VMSystem.Folder.Name

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "OS" -value $VMSystem.Guest.OSFullName

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Power State" -value $VMSystem.PowerState

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "CPU" -value $VMSystem.NumCpu

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Cores Per Socket" -value $VMSystem.CoresPerSocket

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Memory" -value $VMSystem.MemoryGB

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "# Drives Attached" -value $VMDisk.Length

        Add-Member -inputObject $infoObject2 -memberType NoteProperty -name "Guest Disk" -value $VMDiskInfo

        $VMData2 += $infoObject2

 

What I am trying to do is for guest disk is pull info from all of the disks basically just path, Capacity GB, Freespace in GB, and freespace %. Have it populate across columns so one server is on a row.

Something like

 

Disk 1 Cap | Disk 1 Free | Disk 1 Free% | Disk 2 Cap | etc

 

for how ever many disks are connected. That is part of the issue as there can be 1 disk or there can be 10+ in our environment.

 

I did try and use an array to pull the disk info and it grabs it correctly, I am just not sure how to format it correctly to export on each on a new row

 

$VMDisk = $VMSystem.Guest.Disks| Sort-Object -Property path

        foreach ($Disk in $VMDisk)

        {

        $GuestDisk = New-Object PSObject

        Add-Member -inputObject $GuestDisk -memberType NoteProperty -Name "Disk$($index) path" -value $Disk.Path

        Add-Member -inputObject $GuestDisk -memberType NoteProperty -Name "Disk$($index) Capacity(GB)" -Value ([math]::Round($Disk.Capacity/ 1GB))

        Add-Member -inputObject $GuestDisk -memberType NoteProperty -Name "Disk$($index) FreeSpace(GB)" -Value ([math]::Round($Disk.FreeSpace / 1GB))

        Add-Member -inputObject $GuestDisk -memberType NoteProperty -Name "Disk$($index) FreeSpace(%)" -Value ([math]::Round(((100* ($Disk.FreeSpace))/ ($Disk.Capacity)),0))

        $index++

        }

        $VMDiskInfo = $VMDisk += $GuestDisk

 

$VMDiskInfo indexes and displays the data correctly for every disk it sees.

 

I know I can use the $VMDiskInfo[0].Path etc for each disk but if I put 10 of those lines in Add-Member and some are null it will throw an error. and I find myself writing more error correcting lines then I really want to as I have ErrorAction set to stop so I can catch and report them.

I would like to do the same for each nic attached but for now I just wrote some if / else statements to correct errors. I have attached what I have so far. If anyone could point in the right direction my head would be grateful as I keep banging on the desk.

 

Also if anyone sees some efficiencies that can be made i am open to suggestions.

 

Thanks!!!!


Viewing all articles
Browse latest Browse all 175326

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>