VMware Cloud Community
VMwareGuy007
Contributor
Contributor
Jump to solution

Help with formatting on objects

I have a script that I modified from an Alan Renouf script (Thanks for posting the script), but I'm having an issue with the output.

The script is getting some details on a VMs hard disk, and here is what gets shown:

VM                : VM1

Path              : {C:\, D:\}

CapacityGB        : {40, 40}

FreespaceGB       : {1, 18}

Percent free      : {2, 45}

Low Disk space on : C:\

VM                : VM2

Path              : {/, /boot, /usr, /home...}

CapacityGB        : {4, 1, 10, 2...}

FreespaceGB       : {3, 1, 4, 0...}

Percent free      : {84, 95, 40, 17...}

Low Disk space on : /home/marks

The script is below, and here is the main problem.  I can show all the data if I use -join ", " on the parts that need, but I have an if statement that will add another object for "Low Disk space on" and then the drive letter.  I have this for drives that have less than 15 percent available.  If I use -join, the output changes to a string, and the if statement which I put in quotes below

"

if ($HDdetailsHT."Percent free" -lt 15) {

  $HDdetailsHT.Add("Low Disk space on", "$(($HD | where { $_.FreeSpaceGB / $_.CapacityGB * 100 -as [int] -lt 15 }).Path)")

}

"

The above if statement won't work if I modify one of the lines...for instance:

  'CapacityGB' = ($HD.CapacityGB | foreach { $_ -as [int] }) -join ", "


If I do the above, all the data shows, but the if statement no longer works, since now there is string data.  I can't think of a way to use my if statement, and show all the data fully.  Since some VMs may not be low on space, I don't want the "Low Disk space on" to show on everything.  Any help is appreciated. 

$AllHDdetails = @()

$AllVMs = Get-VM 'VM1', 'VM2'

foreach ($vm in $AllVMs) {

$HD = $vm.Guest.Disks | Sort Path

$HDdetailsHT = [ordered]@{

  'VM' = $vm.Name

  'Path' = $HD.Path

  'CapacityGB' = $HD.CapacityGB | foreach { $_ -as [int] }

  'FreespaceGB' = $HD.FreespaceGB | foreach { $_ -as [int] }

  'Percent free' = $HD | foreach { ($_.FreeSpaceGB / $_.CapacityGB) * 100 -as [int] }

}

if ($HDdetailsHT."Percent free" -lt 15) {

  $HDdetailsHT.Add("Low Disk space on", "$(($HD | where { $_.FreeSpaceGB / $_.CapacityGB * 100 -as [int] -lt 15 }).Path)")

}

$HDdetails = New-Object PSObject -Property $HDdetailsHT

$AllHDdetails += $HDdetails

}

$AllHDdetails

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you want it only when there is low disk space, and if you can live with object with differing properties being produced, you could do something like this

$HDdetailsHT = [ordered]@{

  'VM' = $vm.Name

  'Path' = $HD.Path

  'CapacityGB' =  ($HD.CapacityGB | foreach { $_ -as [int] }) -join ", "

  'FreespaceGB' = ($HD.FreespaceGB | foreach { $_ -as [int] }) -join ","

  'Percent free' = ($HD | foreach { ($_.FreeSpaceGB / $_.CapacityGB) * 100 -as [int] }) -join ","

  }

}

$low = $HD | where{($_.FreeSpaceGB / $_.CapacityGB * 100 -as [int]) -lt 15} | Select -ExpandProperty Path

if($low){

    Add-Member -InputObject $HDdetailsHT -Name 'Low Disk space on' -Value $low -MemberType NoteProperty


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$HDdetailsHT = [ordered]@{

  'VM' = $vm.Name

  'Path' = $HD.Path

  'CapacityGB' =  ($HD.CapacityGB | foreach { $_ -as [int] }) -join ", "

  'FreespaceGB' = ($HD.FreespaceGB | foreach { $_ -as [int] }) -join ","

  'Percent free' = ($HD | foreach { ($_.FreeSpaceGB / $_.CapacityGB) * 100 -as [int] }) -join ","

  'Low Disk space on' = ($HD | where{($_.FreeSpaceGB / $_.CapacityGB * 100 -as [int]) -lt 15} | Select -ExpandProperty Path-join ","

  }

}


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

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

Thank you so much LucD.  The only problem I have doing that is that I want the "Low Disk space on" to be dynamic, I only want that object to show if the space is low.  If I make that change, regardless of the disk space, it will always show "Low Disk Space on", but if the space isn't low, it won't have a value. 

I don't want that section showing if the disk space isn't low. Thanks for the help, as always. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is it the intention to export the data to a CSV ?

Then it is advisable to have the same properties on each object in the array.

How and where do you want to report the results ?

Depending on the output medium, it would be easier to filter out the result depending on the content.


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

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

I was just taking the results and outputting them to a text file.  I did try and use csv output, but it didn't help any.  Which would produce the best results?  For me, the main part was just being able to add the "Low disk space" line dynamically, so if everything has to be changed to do that, I'm all for it. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want it only when there is low disk space, and if you can live with object with differing properties being produced, you could do something like this

$HDdetailsHT = [ordered]@{

  'VM' = $vm.Name

  'Path' = $HD.Path

  'CapacityGB' =  ($HD.CapacityGB | foreach { $_ -as [int] }) -join ", "

  'FreespaceGB' = ($HD.FreespaceGB | foreach { $_ -as [int] }) -join ","

  'Percent free' = ($HD | foreach { ($_.FreeSpaceGB / $_.CapacityGB) * 100 -as [int] }) -join ","

  }

}

$low = $HD | where{($_.FreeSpaceGB / $_.CapacityGB * 100 -as [int]) -lt 15} | Select -ExpandProperty Path

if($low){

    Add-Member -InputObject $HDdetailsHT -Name 'Low Disk space on' -Value $low -MemberType NoteProperty


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

0 Kudos
VMwareGuy007
Contributor
Contributor
Jump to solution

You always help me to think in a different way.  I had one problem, but I resolved it.

I'm not sure, but get-member on my hash table showed showed an OrderedDictionary.  I could not use Add-Member and then add to the hash table, but I guess it's because it's not a PSCustom Object.  I used this instead:

$HDdetailsHT.Add("Low Disk space on", "$($low)")

Of course, the major part about using $Low and setting it to the low space is what I needed, I just could not think of that.  Overall, it's working, and I really appreciate the help.

0 Kudos