问题
I am trying to write a very simple Powershell script that checks files in a location and then decides if any are older than 90 days. Easy but it would be good if I can use PS's send-mailmessage cmdlet to use the hash table I have created and accept the contents formatted into HTML to be use in inside a pre build email template.
I have put together the following script (with the table part lifted from another post on 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
I am new to Powershell but have experience with Bash and Perl plus a few others but I just can't see how to do this one.
Any ideas would be greatly appreciated.
回答1:
So looking at this from a Powershell coders view point rather than a shell scripter I decided to build a custom object:
$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
}
}
That then allowed me to loop round the object and convert it into HTML (I could probably play around with ConvertTo-Html but I will look at that later):
# 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>"
I think I might even go back and convert Path and FileName to a URL but the above answers my original question.
来源:https://stackoverflow.com/questions/25536301/powershell-hash-table-to-html