磁盘 Space 报告的 Powershell
Powershell for Disk Space Reports
我正在尝试为我的所有服务器上的磁盘 Space 报告编写 PowerShell。目的是将其 运行 作为每天午夜的计划任务,该任务将导出 CSV 文件并在桌面 space 低于 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 = "<doNotReply@universalexplorer.net>"
To = "<jacob.pagano@universalexplorer.net>"
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'}
您的代码没有将结果捕获到变量中,因此之后将很难测试您是否需要发送电子邮件。 (您现在需要再次执行 Get-WmiObject
)。
此外,您在 Where-Object {($_.freespace/$_.size) -le '0.1'}
中将数字与字符串进行比较
如果我理解这个问题,代码应该总是创建一个报告 csv 文件。
如果报告显示磁盘的可用磁盘少于 10% space,则应发送电子邮件。
如果是每周邮件的标准日,也应发送此电子邮件。
尝试:
$OldReports = (Get-Date).AddDays(-30).Date # set this to midnight
$LogDate = '{0:yyyyMMddhhmm}' -f (Get-Date)
$logPath = '\universalexplorer.net\REPORTS\ServerStorageReports'
$logFile = Join-Path -Path $logPath -ChildPath ('DiskReport_{0}_{1}.csv' -f $env:COMPUTERNAME, $logDate)
# remove all old reports
Get-ChildItem -Path $logPath -Filter "DiskReport_$env:COMPUTERNAME*.csv" -File |
Where-Object { $_.LastWriteTime -le $OldReports} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# get the disk info and capture the results in variable $result
$result = 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)}}
# output the report in a csv file. This will now of course be the most recent
$result | Export-Csv -Path $logFile -NoTypeInformation
# check the results to see if there are disks with less than 10% free space
$lowOnSpace = $result | Where-Object { [int]($_.'Free Space (%)' -replace '\D') -lt 10 }
# if today is the day to send the weekly report. For demo I'll use Monday
# also send if any of the disks have less than 10% free space
if (($lowOnSpace) -or (Get-Date).DayOfWeek -eq 'Monday') {
# do the mailing stuff if needed
$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 $logPath, but the most recent is sent weekly"
From = "<doNotReply@universalexplorer.net>"
To = "<jacob.pagano@universalexplorer.net>"
Attachments = $logFile
SmtpServer = "smarthost.universalexplorer.net"
BodyAsHtml = $true
}
Send-MailMessage @messageParameters
}
希望对您有所帮助
我正在尝试为我的所有服务器上的磁盘 Space 报告编写 PowerShell。目的是将其 运行 作为每天午夜的计划任务,该任务将导出 CSV 文件并在桌面 space 低于 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 = "<doNotReply@universalexplorer.net>"
To = "<jacob.pagano@universalexplorer.net>"
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'}
您的代码没有将结果捕获到变量中,因此之后将很难测试您是否需要发送电子邮件。 (您现在需要再次执行 Get-WmiObject
)。
此外,您在 Where-Object {($_.freespace/$_.size) -le '0.1'}
如果我理解这个问题,代码应该总是创建一个报告 csv 文件。
如果报告显示磁盘的可用磁盘少于 10% space,则应发送电子邮件。
如果是每周邮件的标准日,也应发送此电子邮件。
尝试:
$OldReports = (Get-Date).AddDays(-30).Date # set this to midnight
$LogDate = '{0:yyyyMMddhhmm}' -f (Get-Date)
$logPath = '\universalexplorer.net\REPORTS\ServerStorageReports'
$logFile = Join-Path -Path $logPath -ChildPath ('DiskReport_{0}_{1}.csv' -f $env:COMPUTERNAME, $logDate)
# remove all old reports
Get-ChildItem -Path $logPath -Filter "DiskReport_$env:COMPUTERNAME*.csv" -File |
Where-Object { $_.LastWriteTime -le $OldReports} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# get the disk info and capture the results in variable $result
$result = 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)}}
# output the report in a csv file. This will now of course be the most recent
$result | Export-Csv -Path $logFile -NoTypeInformation
# check the results to see if there are disks with less than 10% free space
$lowOnSpace = $result | Where-Object { [int]($_.'Free Space (%)' -replace '\D') -lt 10 }
# if today is the day to send the weekly report. For demo I'll use Monday
# also send if any of the disks have less than 10% free space
if (($lowOnSpace) -or (Get-Date).DayOfWeek -eq 'Monday') {
# do the mailing stuff if needed
$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 $logPath, but the most recent is sent weekly"
From = "<doNotReply@universalexplorer.net>"
To = "<jacob.pagano@universalexplorer.net>"
Attachments = $logFile
SmtpServer = "smarthost.universalexplorer.net"
BodyAsHtml = $true
}
Send-MailMessage @messageParameters
}
希望对您有所帮助