Powershell 哈希表到 HTML

2024-01-12

我正在尝试编写一个非常简单的 Powershell 脚本,用于检查某个位置中的文件,然后确定是否有超过 90 天的文件。很简单,但如果我可以使用 PS 的 send-mailmessage cmdlet 来使用我创建的哈希表并接受格式化为 HTML 的内容以便在预构建电子邮件模板中使用,那就太好了。

我整理了以下脚本(表格部分取自 StackOverflow 上的另一篇文章 -http://stackoverflow.com/questions/11263538/powershell-display-table-in-html-email 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 以及其他一些方面的经验,但我就是不知道如何做到这一点。

任何想法将不胜感激。


因此,从 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,但上面回答了我原来的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell 哈希表到 HTML 的相关文章

随机推荐