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