我正在尝试编写一个非常简单的Powershell脚本来检查某个位置的文件,然后决定是否有超过90天的文件。很容易,但如果我可以使用PS的send-mailmessage cmdlet来使用我创建的哈希表并接受格式化为HTML的内容以便在预构建的电子邮件模板中使用,那将是很好的。
我已将以下脚本放在一起(表格部分从StackOverflow上的另一个帖子中解除 - http://stackoverflow.com/questions/11263538/powershell-display-table-in-html-email):
$ErrorActionPreference = "SilentlyContinue"
$Path = "Z:\dokuwiki-20140505-0\apps\dokuwiki\data\pages"
$Days = 90
$Now = Get-Date
$ExePath="TBC"
# Set up Functions
function MailSend
{
param([string]$message,[string]$days,[string]$html)
#$message and $html are called inside a HTML format file called Send-Email.html
try
{
# Read html test file and interpolate the variables
$body = iex('"' + (Get-Content "$ExePath\Send-Email.html") + '"')
# Set mail message parameters
$messageParameters = @{
SmtpServer = "smtpserver"
From = "sender"
To = "recipient1"
Cc = "recipient2"
Subject = "DokuWiki files have not been modified in $days"
body = $body
}
Send-MailMessage @messageParameters -BodyAsHtml
}
catch
{
Write-Warning "Unable to send email alert: $_"
}
} #MailSend
# Get list of files to transfer
$FileList = Get-ChildItem -Path $Path -Recurse | Where-Object { $_.mode -notmatch 'd'}
if ($FileList -eq $null)
{
# No files to process
Write-Host "No files found - This is probably a problem"
exit
}
# Find the objects available using Select-Object
#$FileList | Select-Object
# Check there are files older than $Days
foreach ($File in $FileList)
{
#Write-Host $File.Name
$MTime=$File.LastWriteTime
if(($Now - $MTime).Days -ge $Days)
{
$matches.Add($File.Directory,$File)
}
}
# Create a DataTable
$table = New-Object system.Data.DataTable "AgedFiles"
$col1 = New-Object system.Data.DataColumn Path,([string])
$col2 = New-Object system.Data.DataColumn File,([string])
$table.columns.add($col1)
$table.columns.add($col2)
#How do I populate the table?
#How to I get it into HTML format.
# Create an HTML version of the DataTable
$html = "<table><tr><td>Path</td><td>Table</td></tr>"
foreach ($row in $table)
{
$html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td></tr>"
}
$html += "</table>"
#This section prints out the Hash Array to STD-OUT and is to be removed
foreach($item in $matches.GetEnumerator() | Sort-Object Value)
{
if ($item.Value -ne "d")
{
write-host "$($item.name)\$($item.Value)"
}
}
MailSend "This is the message that will appear inside the email body" $days $html
exit
我是Powershell的新手但是对Bash和Perl以及其他一些人有经验,但我不知道如何做到这一点。
非常感谢任何想法。
答案 0 :(得分:2)
因此,从Powershell编码器视图而不是shell脚本编写器看,我决定构建一个自定义对象:
$matches = @()
foreach ($File in $FileList)
{
if(($Now - $File.LastWriteTime).Days -ge $Days)
{
$match = New-Object -TypeName PSObject
$match | Add-Member -Type NoteProperty -Name Path -Value $File.Directory
$match | Add-Member -Type NoteProperty -Name FileName -Value $File
$match | Add-Member -Type NoteProperty -Name ModTime -Value $File.LastWriteTime
$matches += $match
}
}
然后允许我循环对象并将其转换为HTML(我可能会使用ConvertTo-Html,但我稍后会看到):
# Create an HTML version of the DataTable
$html = "<table><tr><td>Path</td><td>FileName</td><td>ModTime</td></tr>"
foreach ($row in $matches)
{
$html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td><td>" + $row[2] + "</td></tr>"
}
$html += "</table>"
我想我甚至可以返回并将Path和FileName转换为URL,但上面的内容回答了我原来的问题。