Automation

 View Only
  • 1.  Help with formatting on objects

    Posted Jun 08, 2015 04:58 PM

    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



  • 2.  RE: Help with formatting on objects

    Posted Jun 08, 2015 08:36 PM

    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 ","

      }

    }



  • 3.  RE: Help with formatting on objects

    Posted Jun 08, 2015 09:44 PM

    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. 



  • 4.  RE: Help with formatting on objects

    Posted Jun 09, 2015 04:48 AM

    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.



  • 5.  RE: Help with formatting on objects

    Posted Jun 09, 2015 11:54 AM

    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. 



  • 6.  RE: Help with formatting on objects
    Best Answer

    Posted Jun 09, 2015 01:05 PM

    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



  • 7.  RE: Help with formatting on objects

    Posted Jun 10, 2015 04:09 PM

    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.