这是我删除的代码:
$myFolder = "C:\Users\myName\Documents\Temporary"
$ReceivedCount = 0
$a = "<style>BODY{font-family: Verdana; font-size: 9pt;}"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }"
$a = $a + "TH{border-width: 2px;padding: 7px;border-style: solid;border-color: black;background-color:lightblue;padding-right: 2px;}"
$a = $a + "TD{border-width: 2px;padding: 5px;border-style: solid;border-color: black;background-color:white; padding-right: 2px;}"
$a = $a + "</style>"
foreach ($file in $myFolder)
{
$FileName = $file.name
Echo "Curent file: $FileName"
if($FileName -like "*.txt")
{
$ReceivedCount += 1
# This is the section I'm doing wrong:
$FilesReceived += @(Select-Object name , length | ConvertTo-HTML -head $a)
}
}
Echo "Found $ReceivedCount files."
if ($FilesReceivedCount -gt 0)
{
#send email...
}
答案 0 :(得分:3)
尝试以下方法:
#$FilesReceived = @()
$FilesReceived += @($file | Select-Object name , length)
$html = $FilesReceived | Convertto-html -head $a
如果你使用gci的-include
参数,你可以事先过滤文件选择:
Get-Childitem C:\path\*.* -Include *.txt
所以你只处理.txt文件
答案 1 :(得分:1)
如果您只想在表格中包含这些文件(以后不再在数组中使用它们),您可以使用:
$filenames = Get-ChildItem -Path $myFolder "*.txt" | Select-Object Name | ConvertTo-Html -Fragment
这将创建一个仅包含文件名的表(如果需要,可以向select-object添加参数)。我在get-childitem上使用管道选择对象而不是-name标志,因为标志将包含完整路径,而select-object只包含名称。