Update check-drive-space.ps1 and install-updates.ps1

This commit is contained in:
Markus Fleschutz 2024-01-24 13:03:18 +01:00
parent beafdaa629
commit 830e211cd4
2 changed files with 48 additions and 26 deletions

View File

@ -1,37 +1,56 @@
<# <#
.SYNOPSIS .SYNOPSIS
Checks a drive for free space left Checks the drive space
.DESCRIPTION .DESCRIPTION
This PowerShell script checks a drive for free space left (20 GB by default). This PowerShell script checks the given drive for free space left (10 GB by default).
.PARAMETER Drive .PARAMETER driveName
Specifies the drive to check Specifies the drive name to check (e.g. "C")
.PARAMETER MinLevel .PARAMETER minLevel
Specifies the minimum level in Gigabyte Specifies the minimum level in bytes (10GB by default)
.EXAMPLE .EXAMPLE
PS> ./check-drive-space C PS> ./check-drive-space.ps1 C
172 GB left on drive C (61 of 233 GB used) Drive C: uses 56%, 442GB free of 999GB
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$Drive = "", [int]$MinLevel = 20) # minimum level in GB param([string]$driveName = "", [int64]$minLevel = 10 * 1000 * 1000) # GB
function Bytes2String { param([int64]$bytes)
if ($bytes -lt 1000) { return "$bytes bytes" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)KB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)MB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)GB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)TB" }
$bytes /= 1000
return "$($bytes)PB"
}
try { try {
if ($Drive -eq "" ) { $Drive = read-host "Enter drive to check" } if ($driveName -eq "" ) { $driveName = Read-Host "Enter the drive name to check" }
$DriveDetails = (get-psdrive $Drive) $details = (Get-PSDrive $driveName)
[int]$Free = (($DriveDetails.Free / 1024) / 1024) / 1024 if (-not $IsLinux) { $driveName = $driveName + ":" }
[int]$Used = (($DriveDetails.Used / 1024) / 1024) / 1024 [int64]$free = $details.Free
[int]$Total = ($Used + $Free) [int64]$used = $details.Used
[int64]$total = ($used + $free)
if ($Free -lt $MinLevel) { if ($total -eq 0) {
write-warning "Drive $Drive has only $Free GB left to use! ($Used of $Total GB used, minimum is $MinLevel GB)" Write-Host "✅ Drive $driveName is empty"
exit 1 } elseif ($free -eq 0) {
} Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is full"
} elseif ($free -lt $minLevel) {
& "$PSScriptRoot/speak-english.ps1" "Drive $Drive has $Free GB left ($Total GB total)" Write-Host "⚠️ Drive $driveName with $(Bytes2String $total) is nearly full, $(Bytes2String $free) free"
} else {
[int]$percent = ($used * 100) / $total
Write-Host "✅ Drive $driveName uses $percent%, $(Bytes2String $free) free of $(Bytes2String $total)"
}
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -16,16 +16,19 @@ try {
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
if ($IsLinux) { if ($IsLinux) {
"⏳ (1/4) Querying updates for installed Debian packages..." "⏳ (1/5) Checking drive space..."
& "$PSScriptRoot/check-drive-space.ps1" /
"⏳ (2/5) Querying latest package information..."
& sudo apt update & sudo apt update
"⏳ (2/4) Upgrading installed Debian packages..." "⏳ (3/5) Removing obsolete packages..."
& sudo apt upgrade --yes
"⏳ (3/4) Removing obsolete Debian packages..."
& sudo apt autoremove --yes & sudo apt autoremove --yes
"⏳ (4/4) Upgrading installed Snap packages..." "⏳ (4/5) Upgrading installed packages..."
& sudo apt upgrade --yes
"⏳ (5/5) Upgrading installed Snaps..."
& sudo snap refresh & sudo snap refresh
} elseif ($IsMacOS) { } elseif ($IsMacOS) {
Write-Progress "⏳ Installing updates..." Write-Progress "⏳ Installing updates..."