Pages - Menu

2019年5月17日 星期五

[PowerShell]偵測硬碟空間大小,並發送Line Notify

為了因應AWS的機器,
所以要盡量將硬碟空間開的剛剛好,但又怕資料塞報硬碟,
只好定時寄送通知了。

根據 使用 Windows PowerShell 顯示本機磁碟機的空間狀態
知道怎麼顯示 硬碟空間大小
再根據條件判斷何時要發送Line Notify,
Line Notify的發送方式,請參考 PowerShell 檢查硬碟狀態發送line通知


# 取得所有硬碟的資訊
$Disks = Get-WmiObject -Class Win32_LogicalDisk

# 輸出每一個硬碟的資訊
foreach ($Disk in $Disks) {
  "------------"
  "磁碟機代碼:{0}" -f $Disk.DeviceID
  "磁碟機名稱:{0}" -f $Disk.VolumeName
  "磁碟機大小:{0:0.0} GB" -f ($Disk.Size / 1GB)
  "剩餘空間:{0:0.0} GB" -f ($Disk.FreeSpace / 1GB)
  $Used = ([int64]$Disk.size - [int64]$Disk.FreeSpace)
  "已用空間:{0:0.0} GB" -f ($Used / 1GB)
  $Percent = ($Used * 100.0) / $Disk.Size
  "已用比例:{0:N0} %" -f $Percent
  if($Disk.DeviceID -eq "C:")
  {
 
    "Get C"
    $Percent
    if($Percent -gt 40){
        "C > 50 %"
        line_notify
    } 
    break
 
  }else{
    "Get Zero"
  } 
}

上面有幾點要注意,
PowerShell的 >= <= 使用的方式是 -gt -lt
這邊只列幾個常用的

等於            -eq
不等於        -ne
大於等於    -ge
大於            -gt
小於            -lt
小於等於    -le


完整運算子,請參考 限量ㄟ蓋步-PowerShell - 運算子

基本規則,請參考黑暗執行緒-Powershell 學習筆記

沒有留言:

張貼留言