0
我一直在瀏覽網頁,試圖找到一種方法,如果可能的話,從Gmail帳戶通過電子郵件發送到共享郵箱的磁盤空間不足警報,但我正在努力與一個查詢我已經拼湊起來了。通過電子郵件發送硬盤磁盤空間警報使用Powershell
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("Username", "Password");
$Computers = "Local Computer"
$Subject = "Disk Space Storage Report"
$Body = "This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed."
[decimal]$thresholdspace = 50
$tableFragment = Get-WMIObject -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3} `
| ConvertTo-HTML -fragment
$regexsubject = $Body
$regex = [regex] '(?im)<td>'
if ($regex.IsMatch($regexsubject)) {$smtpclinet.send($fromemail, $EmailTo, $Subject, $Body)}
腳本運行但沒有任何反應,任何幫助將是太棒了!
將分而治之的策略運用到調試中。分解腳本並找出哪部分失敗。 WMI查詢是否報告了良好的結果?你可以通過Gmail發送任意郵件嗎? – vonPryz
有錯別字不會幫助'smtpclinet'。爲什麼不使用Send-MailMessage?無論如何,你一無所獲,因爲你永遠不會將'$ tableFragment'合併到'$ Body'中。如果'$ Body'不包含具有表格數據的HTML表格,則郵件永遠不會被髮送。 –
您的另一個問題。 '$ Body'構建了一個'$ thresholdspace'變量。 '$ thesholdspace'變量在'$ Body'已經使用該值之後才被設置。 –