PowerShell哈希表到Html的转换 [英] Powershell Hash Table to HTML

查看:8
本文介绍了PowerShell哈希表到Html的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个非常简单的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-0appsdokuwikidatapages"
$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 "$ExePathSend-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>"

我想我甚至可以返回并将路径和文件名转换为URL,但上面的内容回答了我最初的问题。

这篇关于PowerShell哈希表到Html的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆