Extacting Capacity Info using Powershell Regex

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 3 weeks, 5 days ago.

  • Author
    Posts
  • #57559
    Profile photo of Amar Helloween
    Amar Helloween
    Participant

    Hi All,

    Below is the Data i have fetched from a Linux machine using Invoke-SSHCommand. I have stored the value in 1 variable. The task is to extract the capacity info(which is in somevalue% format) and check it should be less than 85%, if anything is greater than which filesystem(not mandatory but it will be helpful). So kindly help me with a Regex to perform the same.

    Data is :

    Filesystem kbytes used avail capacity Mounted on
    /dev/md/dsk/d100 10332220 4948160 5280738 49% /
    /devices 0 0 0 0% /devices
    ctfs 0 0 0 0% /system/contract
    proc 0 0 0 0% /proc
    mnttab 0 0 0 0% /etc/mnttab
    swap 3327032 1672 3325360 1% /etc/svc/volatile
    objfs 0 0 0 0% /system/object
    sharefs 0 0 0 0% /etc/dfs/sharetab
    /platform/SUNW,T5140/lib/libc_psr/libc_psr_hwcap2.so.1 10332220 4948160 5280738 49% /platform/sun4v/lib/libc_psr.so.1
    /platform/SUNW,T5140/lib/sparcv9/libc_psr/libc_psr_hwcap2.so.1 10332220 4948160 5280738 49% /platform/sun4v/lib/sparcv9/libc_psr.so.
    1
    fd 0 0 0 0% /dev/fd
    /dev/md/dsk/d103 10332220 6723449 3505449 66% /var
    swap 524288 32456 491832 7% /tmp
    swap 3325400 40 3325360 1% /var/run
    /dev/md/dsk/d104 5170926 1224897 3894320 24% /opt
    /dev/md/dsk/d61 10326524 4070128 6153131 40% /spa
    /dev/md/dsk/d62 90877410 36910561 53058075 42% /spare
    /dev/md/dsk/d5 5171190 3370847 1748632 66% /export/home
    dc1c9e-pool/ofs01 140378112 58053732 82315069 42% /ofs01

  • #57584
    Profile photo of Alexandru
    Alexandru
    Participant

    This is what i came up with , i'm sure that there are other ways to do this and maybe better

    PS C:\Users\user\Desktop> Get-Content .\df.txt
    Filesystem kbytes used avail capacity Mounted on
    /dev/md/dsk/d100 10332220 4948160 5280738 49% /
    /devices 0 0 0 0% /devices
    ctfs 0 0 0 0% /system/contract
    proc 0 0 0 0% /proc
    mnttab 0 0 0 0% /etc/mnttab
    swap 3327032 1672 3325360 1% /etc/svc/volatile
    objfs 0 0 0 0% /system/object
    sharefs 0 0 0 0% /etc/dfs/sharetab
    /platform/SUNW,T5140/lib/libc_psr/libc_psr_hwcap2.so.1 10332220 4948160 5280738 49% /platform/sun4v/lib/libc_psr.so.1
    /platform/SUNW,T5140/lib/sparcv9/libc_psr/libc_psr_hwcap2.so.1 10332220 4948160 5280738 49% /platform/sun4v/lib/sparcv9/libc_psr.so.1
    fd 0 0 0 0% /dev/fd
    /dev/md/dsk/d103 10332220 6723449 3505449 66% /var
    swap 524288 32456 491832 7% /tmp
    swap 3325400 40 3325360 1% /var/run
    /dev/md/dsk/d104 5170926 1224897 3894320 24% /opt
    /dev/md/dsk/d61 10326524 4070128 6153131 40% /spa
    /dev/md/dsk/d62 90877410 36910561 53058075 42% /spare
    /dev/md/dsk/d5 5171190 3370847 1748632 66% /export/home
    dc1c9e-pool/ofs01 140378112 58053732 82315069 42% /ofs01

    Get only the capacity column , however this Import-Csv command will create two columns out of 'Mounted on' because it's splitting on space , you can just ignore it since it doesn't really matter in this case.

    PS C:\Users\user\Desktop> Import-Csv .\df.txt -Delimiter " " | ForEach-Object { 'Capacity {0}' -f $_.capacity }
    Capacity 49%
    Capacity 0%
    Capacity 0%
    Capacity 0%
    Capacity 0%
    Capacity 1%
    Capacity 0%
    Capacity 0%
    Capacity 49%
    Capacity 49%
    Capacity 0%
    Capacity 66%
    Capacity 7%
    Capacity 1%
    Capacity 24%
    Capacity 40%
    Capacity 42%
    Capacity 66%
    Capacity 42%

    Or , get more info out.
    First , i'm removing the '%' from the capacity number so i can convert it to integer and do a comparison with another integer , and then format my output with the format operator (-f)

    PS C:\Users\user\Desktop> Import-Csv .\df.txt -Delimiter " " |  Where-Object { [int]($_.capacity.replace('%','')) -gt 40 } | ForEach-Object { 'Filesystem: {0,-80} Capacity {1}' -f $_.Filesystem,$_.capacity }
    Filesystem: /dev/md/dsk/d100                                                                 Capacity 49%
    Filesystem: /platform/SUNW,T5140/lib/libc_psr/libc_psr_hwcap2.so.1                           Capacity 49%
    Filesystem: /platform/SUNW,T5140/lib/sparcv9/libc_psr/libc_psr_hwcap2.so.1                   Capacity 49%
    Filesystem: /dev/md/dsk/d103                                                                 Capacity 66%
    Filesystem: /dev/md/dsk/d62                                                                  Capacity 42%
    Filesystem: /dev/md/dsk/d5                                                                   Capacity 66%
    Filesystem: dc1c9e-pool/ofs01                                                                Capacity 42%
    • #57587
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Wow. Don't you feel like you just did someones homework now? 😉

  • #57590
    Profile photo of Alexandru
    Alexandru
    Participant

    Pretty much :))
    But this is the only way i'm actually learning powershell , because my memory doesn't really help , I need practice

    • #57593
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Ah ... ok. So at least someone's got help. 😉 😀

You must be logged in to reply to this topic.