我正在尝试在所有服务器上编写用于磁盘空间报告的PowerShell。目的是使其在每天的午夜作为计划任务运行,这将导出CSV并在桌面空间不足10%时发送电子邮件,该电子邮件将包含最新CSV的附件报告。
我遇到的问题是电子邮件部分。整个代码如下!
$OldReports = (Get-Date).AddDays(-30)
#edit the line below to the location you store your disk reports# It might also
#be stored on a local file system for example, D:\ServerStorageReport\DiskReport
$messageParameters = @{
Subject = "Weekly Server Storage Report"
Body = "Attached is Weekly Server Storage Report. The scipt has been amended to return only servers with free disk space less than or equal to 10%. All reports are located in \\universalexplorer.net\REPORTS\ServerStorageReports$Env:COMPUTERNAMEDiskReport\, but the most recent is sent weekly"
From = "<[email protected]>"
To = "<[email protected]>"
Attachments = (Get-ChildItem "\\universalexplorer.net\REPORTS\ServerStorageReports\DiskReport_$Env:COMPUTERNAME*" | sort LastWriteTime | select -last 1)
SmtpServer = "smarthost.universalexplorer.net"
}
Get-ChildItem "\\universalexplorer.net\REPORTS\ServerStorageReports\DiskReport_$Env:COMPUTERNAME*" | `
Where-Object { $_.LastWriteTime -le $OldReports} | `
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
#Create variable for log date
$LogDate = get-date -f yyyyMMddhhmm
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" |
Select-Object -Property DeviceID, DriveType, VolumeName,
@{Label = "Drive Letter";Expression = {$_.DeviceID}},
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f ( $_.Freespace / 1gb ) }},
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} | Export-Csv -path "\\universalexplorer.net\REPORTS\ServerStorageReports\DiskReport_$Env:COMPUTERNAME'_'$logDate.csv" -NoTypeInformation
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | Send-MailMessage @messageParameters -BodyAsHtml | Where-Object {($_.freespace/$_.size) -le '0.1'}
这是电子邮件部分的代码片段。
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | Send-MailMessage @messageParameters -BodyAsHtml | Where-Object {($_.freespace/$_.size) -le '0.1'}