Expected Behavior
the script should instruct my telegram bot to send me a notification when the remaining free space on the drive that i specify is below 70%.
Current Behavior
However the bot always sends this notification, even if there is more than 70% left. Also, it does not specify the actual percentage of free space. I cannot understand where the problem comes from because i checked the $percentFree variables and they all match in the code.
I am learning powershell and scripting so i probably messed up somewhere.
Steps to Reproduce
param (
[Parameter(Mandatory = $true)]
[string]
$Drive
)
#log directory
if ($PSVersionTable.Platform -eq 'Unix') {
$logpath = '/tmp'
}
else {
$logpath = 'Y:\Desktop\Logs'
}
$logfile ="$logpath\driveCheck.log" #logfile
#verify if log directory exists
try {
if (-not (Test-Path -Path $logpath -ErrorAction Stop)) {
#output dir not found. Create the dir
New-Item -ItemType Directory -Path $logpath -ErrorAction Stop | Out-Null
New-Item -ItemType File -Path $logfile -ErrorAction Stop | Out-Null
}
}
catch {
throw
}
Add-Content -Path $logfile -Value '[INFO] Running $PSCommandPath'
#verify if Poshgram is installed
if (-not (Get-Module -Name PoshGram -ListAvailable)) {
Add-Content -Path $logfile -Value '[ERROR] PoshGram is not installed.'
throw
}
else {
Add-Content -Path $logfile -Value '[INFO] PoshGram is installed.'
}
#get hard drive information
try {
if ($PSVersionTable.Platform -eq 'Unix')
{
# used
# free
$volume = Get-PSDrive -Name $Drive -ErrorAction Stop
#verify volume actually exists
if ($volume) {
$total = $volume.Used + $volume.Free
$percentFree = [int](($volume.Free / $total) * 100)
Add-Content -Path $logfile -Value "[INFO] Percent free: $percentFree%."
else {
Add-Content -Path $logfile -Value '[ERROR] Drive is not found.'
throw
else {
$volume = Get-Volume -ErrorAction Stop | Where-Object {$.DriveLetter -eq $Drive}
if ($volume) {
$total = $volume.Size
$percentFree = [int](($volume.SizeRemaining / $total) * 100)
Add-Content -Path $logfile -Value "[INFO] Percent free: $percentFree%."
else {
Add-Content -Path $logfile -Value '[ERROR] Drive is not found.'
throw
}
}
}
}
}
catch {
Add-Content -Path $logfile -Value '[ERROR] Unable to retrieve volume information.'
Add-Content -Path $logfile -Value $
throw
}
#send telegram message if the drive is low
if ($percentFree -le 70) {
try {
Import-Module -Name Poshgram -ErrorAction Stop
Add-Content -Path $logfile -Value '[INFO] Imported Poshgram successfully.'
}
catch {
Add-Content -Path $logfile -Value '[ERROR] Poshgram could not be imported:'
Add-Content -Path $logfile -Value $_
}
Add-Content -Path $logfile -Value '[INFO] Sending Telegram notification.'
$MessageSplat = @{
Message = "[LOW SPACE] Hero, your drive is at: $percentFree%."
ChatID = "-444846118"
BotToken = "1287418460:AAGBCvO75O40-d3IP_oEsTxvVMnnNkhh9Lc"
ErrorAction = 'Stop'
}
try { Send-TelegramTextMessage @MessageSplat
Add-Content -Path $logfile -Value '[INFO] Message sent successfully.'
}
catch {
Add-Content -Path $logfile -Value '[ERROR] Error encountered sending message:'
Add-Content -Path $logfile -Value $_
throw
}
}
Context (Environment)
Name Value
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Microsoft Windows 10.0.18363
Detailed Description
the telegram notification the bot sends me does not show the % of free space on the specified Drive.
driveCheck.log