PowerShell Script That Will Parse Contents Of File And Send Email

I am trying to writ a PowerShell script that will complete the following:

Parse a txt file for percentage of disk used. Here is an example of the file:

Filesystem Size Used Avail Use% Mounted on
devtmpfs 132G 0 132G 0% /dev
tmpfs 132G 0 132G 0% /dev/shm
tmpfs 132G 4.3G 128G 4% /run
tmpfs 132G 0 132G 0% /sys/fs/cgroup
/dev/mapper/sysvg-lv_root 22G 15G 7.2G 68% /
/dev/sda2 1.1G 492M 572M 47% /boot
/dev/sda1 1.1G 6.1M 1.1G 1% /boot/efi
/dev/mapper/sysvg-lv_var 25G 794M 24G 4% /var
/dev/mapper/sysvg-lv_home 11G 392M 11G 4% /home
/dev/mapper/sysvg-lv_opt 26G 498M 26G 2% /opt
/dev/mapper/sysvg-lv_tmp 8.6G 98M 8.5G 2% /tmp
/dev/mapper/sysvg-lv_var_tmp 3.3G 57M 3.2G 2% /var/tmp
/dev/mapper/sysvg-lv_log 8.6G 4.8G 3.9G 55% /var/log
/dev/sdc 206G 40G 155G 21% /opt/App
/dev/sdb 773G 458G 277G 63% /opt/App1
/dev/mapper/sysvg-lv_audit 8.6G 129M 8.5G 2% /var/log/audit
tmpfs 27G 13k 27G 1% /run/user/42
tmpfs 27G 0 27G 0% /run/user/1001

Based off of the Use% column (file is a plain txt file) Set a range of values. Example: (gt 75% and lt 99%) do something such as send an email. Not sure how to parse the file for % values?

  1. $FilePath = “C:\server-diskspace.txt”
  2. $ll = “10%”
  3. $ul = “100%”
  4. $a = “-gt 75”
  5. Get-Content -Path $FilePath
  6. if ($a -gt 4 -and $a -lt 100) { Write-Host “Hello” }

Welcome to the forum. This forum is for helping with specific Powershell questions. We do not write scripts for people. If you have any code you are working with please add that with the specific questions. Also make sure to format the code with the ‘</>’ button.

1 Like

Some pointers
I would change the ‘Mount on’ to something like ‘Mount_on’

Hint after that is Import-CSV with -delimiter ’ ’ would make this very simple

1 Like

First thing about powershell is understanding it’s designed to use objects. In that regard, I’d recommend to start with getting your output in objects.

$output = (df -h | Select-Object -Skip 1)-replace '\s+',',' | ConvertFrom-Csv -Header Filesystem, Size, Used, Avail, Use%, MountPath

That will give you the same data but now in a very easy to use structure. The next thing we need to consider is to compare items appropriately, we need to ensure it is the right data type. Anything surrounded with " " is a string, so that’s the first thing we are going to need to fix. We need an actual number (an integer, to be specific.) In order to get an int from the use% property, we also need to strip it of the % character.

We can do that up front if we like and then compare

$output = (df -h | Select-Object -Skip 1) -replace '\s+',',' | ConvertFrom-Csv -Header Filesystem, Size, Used, Avail, Use%, MountPath
$output | ForEach-Object {[int]$_.'Use%' = $_.'Use%' -replace '%'}
$output | Where-Object {$_.'Use%' -gt 4 -and $_.'Use%' -lt 100}

Or we can do it on the fly during the comparison

$output = (df -h | Select-Object -Skip 1) -replace '\s+',',' | ConvertFrom-Csv -Header Filesystem, Size, Used, Avail, Use%, MountPath
$output | Where-Object {[int]($_.'Use%' -replace '%') -gt 4 -and [int]($_.'Use%' -replace '%') -lt 100}

I think doing it up front is a lot cleaner. It would also be cleaner to use standard characters for the properties so we don’t have to worry about quoting the property name. Like so

$output = (df -h | Select-Object -Skip 1) -replace '\s+',',' | ConvertFrom-Csv -Header Filesystem, Size, Used, Avail, UsePercent, MountPath
$output | ForEach-Object {[int]$_.UsePercent = $_.UsePercent -replace '%'}
$output | Where-Object {$_.UsePercent -gt 4 -and $_.UsePercent -lt 100}

So I am able to use a bas shell script to generate the csv locally on the RHEL server (works perfectly) but when I direct it out to the local disk from remote powershell it isnt in csv format?

plink user@my_host -pw XXXXXXXXX -batch “df -H | awk '{print $1”,“$2”,“$3”,“$4”,“$5”,“$6” “$7}'” > rhelserver-diskspace.csv

Nothing about the command you’re showing now has anything to do with powershell. Why manually create the CSV? Did you even try my suggestion? You’ve missed a comma between your 6th and 7th variable. A side effect of manually creating your own CSV output. Please keep in mind this is a powershell forum. Your initial post didn’t include any code at all, and your second post doesn’t include any powershell code.

1 Like

My apologies. Thanks for the tips and I will continue working on this outside of this forum.

If that’s what you choose. If you are running powershell, you could also try my suggestion. The first command will take the output of df -h and put it in rich objects. The second command transforms the percent values to actual integers. From there you can compare as desired and export effortlessly with Export-CSV. There is no need to manually create CSV with string manipulation. Best of luck either way.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.