-
Notifications
You must be signed in to change notification settings - Fork 0
/
Freespace-DiskDrive.ps1
44 lines (34 loc) · 1.47 KB
/
Freespace-DiskDrive.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# ------------------------------------------------------------------------------------------------------------------
# Name : Freespace-DiskDrive.ps1
# Author : Harsh Arora
# Description : This powershell script will report the free space of the disk drive to a ThingSpeak Channel.
# ------------------------------------------------------------------------------------------------------------------
$Logfile = "$env:SystemDrive\Freespace_DiskDrive.log"
function LogWrite([string]$logstring)
{
$DateTime = Get-Date -Format g
Add-content $Logfile -value "$DateTime $logstring"
}
# Get Disk Size and Free Space
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='D:'" | Select-Object Size,FreeSpace
# Format Free Space into Gigabytes
$disk.FreeSpace = $disk.FreeSpace / 1Gb
$disk.FreeSpace = [math]::Round($disk.FreeSpace, 2)
$disk.FreeSpace
LogWrite "Free Space into GB: $disk.FreeSpace"
# ThingSpeak Write API Key (Replace with your key)
$key = "xxxxxxxxxxxxxxxx"
# ThingSpeak URL
$url = "https://api.thingspeak.com/update?key=" + $key + "&field1=" + $disk.FreeSpace
LogWrite "ThingSpeak URL: '$url'"
# Send Disk Space to ThingSpeak
$HTTP_Client = new-object net.webclient
$HTTP_Response = $HTTP_Client.DownloadString($url)
# Check Rate Limit and Display Results
If ($HTTP_Response -ne '0') {
LogWrite "ThingSpeak Entry ID:" $HTTP_Response
LogWrite "Disk Free Space:" $disk.FreeSpace "GB"
}
else {
LogWrite "Channel Update Failed - Check Rate Limit"
}