locked
Merge Win32_Volume requests to calculate the free space RRS feed

  • Question

  • Hi, 

    I have the following code

    For ($i = 0; $i -le 1; $i++) {
    	Write-Host "server:" $servers[$i]
    	Get-WmiObject -Credential $credencials[$i] –ComputerName $servers[$i] –Class Win32_Volume -Filter "DriveType=3 AND Label != 'System Reserved' AND DriveLetter IS NULL" `
           		| ft –auto `
                      	Label,
                      	@{Label=”Free(GB)”;Expression={“{0:N0}” –F ($_.FreeSpace/1GB)}}`
    }
    

    that generates the following output:

    server: PROXY1
    
    Label          Free(GB)
    -----          --------
    Backup LUN 240 5,687
    Backup LUN 247 5,638
    Backup LUN 248 5,961
    Backup LUN 249 5,641
    Backup LUN 250 5,154
    Backup LUN 251 6,295
    Backup LUN 252 5,939
    Backup LUN 239 4,988
    eLUN188        9,974
    eLUN190        9,923
    
    
    server: PROXY2
    
    Label                 Free(GB)
    -----                 --------
    Backup LUN 238        5,844
    Backup LUN 241        5,658
    Config Backup LUN 242 98
    eLUN189               13,156
    eLUN191               14,775
    Backup LUN 243        6,140
    Backup LUN 244        5,875
    Backup LUN 245        5,719
    Backup LUN 246        5,893

    I want merge both results in order to sum all volumes with name "Backup LUN XXX" and sume all eLUNXXX  to get the free available space on each LUN group, but i'm not capable to merge both requests to make this operations.

     

    Wednesday, August 29, 2018 2:21 PM

All replies

  • Place the FT outside of the loop.


    \_(ツ)_/

    Wednesday, August 29, 2018 5:03 PM
  • A better approach would be this:

    $servers = @(
        @{Name='server1';Credential=$cred1},
        @{Name='server2';Credential=$cred2},
        @{Name='server3';Credential=$cred3}
    )
        
    $servers |
        ForEach-Object{
        	Write-Host server $_.Name
        	Get-WmiObject  Win32_Volume  "DriveType=3 AND Label != 'System Reserved' AND DriveLetter IS NULL" -ComputerName $_.Name -Credential $_.Credential
        } |
        Format-Table Label, @{Label=”Free(GB)”;Expression={“{0:N0}” –F ($_.FreeSpace/1GB)}}
    


    \_(ツ)_/

    Wednesday, August 29, 2018 5:08 PM
  • Hi,

    Thx jrv for you reply, finally i solve with this code, i made it because i have diferent LUN managed by aplication as one disk "3 in reality" so i want get the global used space, i'm not expert in powershell and i know that this provably is not the best way, but i put here, maybe is useful for someone or someone can improve much better.

    $user = "domain\user"
    $passproxy2 = cat credentials1.txt | convertto-securestring
    $passproxy2 = cat credentials2.txt | convertto-securestring
    $cred0 = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$passproxy1
    $cred1 = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$passproxy2
    $servers = @("proxy1","proxy2")
    $credencials = @($cred0,$cred1)
    $lun = @("Backup LUN*","Config Backup*","eLUN*")
    
    
    
    For ($j = 0; $j -le $lun.Length-1; $j++) {
        [float]$lliure = 0;
        [float]$ocupat = 0;
        [float]$total = 0;
        [float]$ocupatpercent = 0;
        For ($i = 0; $i -le 1; $i++) {
    	    $disks = Get-WmiObject -Credential $credencials[$i] –ComputerName $servers[$i] –Class Win32_Volume -Filter "DriveType=3 AND Label != 'System Reserved' AND DriveLetter IS NULL" | Where-Object {$_.label -Like $lun[$j] }
        
            $disctotal = $disks.capacity -split " ";
            foreach ($char in $disctotal) {
                $total = $total + [math]::round(($char / 1GB),2);
            }   
            $disclliure = $disks.FreeSpace -split " ";
            foreach ($char in $disclliure) {
                $lliure = $lliure + [math]::round(($char / 1GB),2);
            }
        
            $ocupat = $total - $lliure;
            $lliurepercent = [math]::round(($lliure / $total),2) * 100
        }
    
        Write-Host "LUN Group" $lun[$j] "Total Disk: " $total "GB - Used Disk: " $ocupat "GB - FREE SPACE: "$lliure "GB " $lliurepercent "%"
    
    }
    
    
    pause

    This is the output

    C:\>getfreespaceallLUN.ps1
    LUN Group Backup LUN* Total Disk:  200704.2 GB - Used Disk:  120083.6 GB - FREE SPACE:  80620.53 GB  40 %
    LUN Group Config Backup* Total Disk:  100.01 GB - Used Disk:  2.120003 GB - FREE SPACE:  97.89 GB  98 %
    LUN Group eLUN* Total Disk:  151551.5 GB - Used Disk:  102582.3 GB - FREE SPACE:  48969.22 GB  32 %

    Thursday, August 30, 2018 10:46 AM
  • Most of your code is unnecessary.  You cn just "sum" the values directly.

    $disctotal = ($disks.capacity | Measure-Object capacity -Sum).Sum

    It is not necessary to declare and assign variables.  Direct assignment always clears the variable.


    \_(ツ)_/

    Thursday, August 30, 2018 1:36 PM