# PowerShell Script That Will Parse Contents Of File And Send Email

**URL:** <https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184>\
**Category:** PowerShell Help\
**Created:** [May 23, 2024, 7:22pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184 "2024-05-23T19:22:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hz36t5](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@hz36t5](https://forums.powershell.org/u/hz36t5)\
**Post date:** [May 23, 2024, 7:22pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/1 "2024-05-23T19:22:58Z")

</div>

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” }

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 23, 2024, 7:32pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/2 "2024-05-23T19:32:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 23, 2024, 7:33pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/3 "2024-05-23T19:33:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 23, 2024, 9:38pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/4 "2024-05-23T21:38:26Z")

</div>

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.

```auto
$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

```auto
$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

```auto
$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

```auto
$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}

```

---

<div class="post-metadata">

**Author:** ![hz36t5](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@hz36t5](https://forums.powershell.org/u/hz36t5)\
**Post date:** [May 25, 2024, 1:28pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/5 "2024-05-25T13:28:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 25, 2024, 2:01pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/6 "2024-05-25T14:01:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hz36t5](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@hz36t5](https://forums.powershell.org/u/hz36t5)\
**Post date:** [May 25, 2024, 2:36pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/7 "2024-05-25T14:36:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [May 25, 2024, 9:11pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/8 "2024-05-25T21:11:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 24, 2024, 9:11pm UTC](https://forums.powershell.org/t/powershell-script-that-will-parse-contents-of-file-and-send-email/24184/9 "2024-06-24T21:11:14Z")

</div>

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