Skip to content

Commit ad389f6

Browse files
committed
Added Ep10 PowerShell Scripts
1 parent 5afaf00 commit ad389f6

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#____________________________________________________________
2+
# https://techthoughts.info/powershell-scripts/
3+
#____________________________________________________________
4+
5+
#region links
6+
7+
#About Execution Policies
8+
#https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-6
9+
10+
#About Scripts
11+
#https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-6
12+
13+
#endregion
14+
15+
#region running scripts
16+
17+
#get the execution policy of all scopes in the order of precedence
18+
Get-ExecutionPolicy -List
19+
20+
#change execution policy
21+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
22+
23+
#unblock a script downloaded from the internet after you have read and understood the code
24+
Unblock-File -Path .\drive_warn.ps1
25+
26+
#run a local script
27+
.\drive_warn.ps1
28+
29+
#run a local script in the current scope
30+
. .\drive_warn.ps1
31+
32+
#endregion
33+
34+
#region script example
35+
36+
param (
37+
[Parameter(Mandatory = $true)]
38+
[string]
39+
$Drive
40+
)
41+
42+
if ($PSVersionTable.Platform -eq 'Unix') {
43+
$logPath = '/tmp'
44+
}
45+
else {
46+
$logPath = 'C:\Logs' #log path location
47+
}
48+
49+
#need linux path
50+
51+
$logFile = "$logPath\driveCheck.log" #log file
52+
53+
#verify if log directory path is present. if not, create it.
54+
try {
55+
if (-not (Test-Path -Path $logPath -ErrorAction Stop )) {
56+
# Output directory not found. Creating...
57+
New-Item -ItemType Directory -Path $logPath -ErrorAction Stop | Out-Null
58+
New-Item -ItemType File -Path $logFile -ErrorAction Stop | Out-Null
59+
}
60+
}
61+
catch {
62+
throw
63+
}
64+
65+
Add-Content -Path $logFile -Value "[INFO] Running $PSCommandPath"
66+
67+
#verify that the required Telegram module is installed.
68+
if (-not (Get-Module -ListAvailable -Name PoshGram)) {
69+
Add-Content -Path $logFile -Value '[INFO] PoshGram not installed.'
70+
throw
71+
}
72+
else {
73+
Add-Content -Path $logFile -Value '[INFO] PoshGram module verified.'
74+
}
75+
76+
#get hard drive volume information and free space
77+
try {
78+
if ($PSVersionTable.Platform -eq 'Unix') {
79+
$volume = Get-PSDrive -Name $Drive -ErrorAction Stop
80+
#verify volume actually exists
81+
if ($volume) {
82+
$total = $volume.Free + $volume.Used
83+
$percentFree = [int](($volume.Free / $total) * 100)
84+
Add-Content -Path $logFile -Value "[INFO] Percent Free: $percentFree%"
85+
}
86+
else {
87+
Add-Content -Path $logFile -Value "[ERROR] $Drive was not found."
88+
throw
89+
}
90+
}
91+
else {
92+
$volume = Get-Volume -ErrorAction Stop | Where-Object { $_.DriveLetter -eq $Drive }
93+
#verify volume actually exists
94+
if ($volume) {
95+
$total = $volume.Size
96+
$percentFree = [int](($volume.SizeRemaining / $total) * 100)
97+
Add-Content -Path $logFile -Value "[INFO] Percent Free: $percentFree%"
98+
}
99+
else {
100+
Add-Content -Path $logFile -Value "[ERROR] $Drive was not found."
101+
throw
102+
}
103+
}
104+
}
105+
catch {
106+
Add-Content -Path $logFile -Value '[ERROR] Unable to retrieve volume information:'
107+
Add-Content -Path $logFile -Value $_
108+
throw
109+
}
110+
111+
#evaluate if a message needs to be sent if the drive is below 20GB freespace
112+
if ($percentFree -le 20) {
113+
114+
try {
115+
Import-Module PoshGram -ErrorAction Stop
116+
Add-Content -Path $logFile -Value '[INFO] PoshGram imported successfully.'
117+
}
118+
catch {
119+
Add-Content -Path $logFile -Value '[ERROR] PoshGram could not be imported:'
120+
Add-Content -Path $logFile -Value $_
121+
throw
122+
}
123+
124+
Add-Content -Path $logFile -Value '[INFO] Sending Telegram notification'
125+
126+
$messageSplat = @{
127+
BotToken = "#########:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
128+
ChatID = "-#########"
129+
Message = "[LOW SPACE] Drive at: $percentFree%"
130+
ErrorAction = 'Stop'
131+
}
132+
133+
try {
134+
Send-TelegramTextMessage @messageSplat
135+
Add-Content -Path $logFile -Value '[INFO] Message sent successfully'
136+
}
137+
catch {
138+
Add-Content -Path $logFile -Value '[ERROR] Error encountered sending message:'
139+
Add-Content -Path $logFile -Value $_
140+
throw
141+
}
142+
143+
}
144+
145+
#endregion

0 commit comments

Comments
 (0)