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'已经使用该值之后才被设置。 –