Powershell out将结果列入表格

时间:2017-12-11 21:23:52

标签: powershell

我正在将文件从源复制到多个位置,然后检查源以查看是否成功复制了文件。 $result会逐行在控制台中列出结果,但是当我尝试将$result发送到电子邮件时,它会以一行中的所有内容结束。我尝试format-tableconverto-htl但没有成功。我想使用hashtable但需要一些指导。

    $Source = "C:\temp\Copy\00_S"
    $DST = "C:\temp\Copy\01_D","C:\temp\Copy\02_D","C:\temp\Copy\03_D"
    $FileList = Get-ChildItem -Path $Source\*.xml | Select -ExpandProperty Name
    $DST | %{Copy-Item $Source\*.xml -Destination $_ -Force}
    $result = Foreach ($item in $FileList){
       $DST | % {if (Test-Path ($_ + "\" +  "$item")){
            "$item exists in $_"
            }else{
            "$item does not exist in $_"
            }
           }
       }

     $result

这是我想要的最终目的。

enter image description here

1 个答案:

答案 0 :(得分:1)

代码

# Initialise array
$results = @()

# Create a hashtable with the attributes you want to track
$result = @{
    Source = "File 1"
    Dest1  = "Yes"
    Dest2  = "Yes"
    Dest3  = "Yes"
}
# Append hashtable to results array
$results += $result

# Rinse-repeat
$result = @{
    Source = "File 2"
    Dest1  = "Yes"
    Dest2  = "Yes"
    Dest3  = "Yes"
}
$results += $result

$result = @{
    Source = "File 3"
    Dest1  = "Yes"
    Dest2  = "Yes"
    Dest3  = "Yes"
}
$results += $result

# Display the results
#  selecting the columns in your desired display order
#  and then converting to a HTML table
$results | Select-Object Source, Dest1, Dest2, Dest3 | ConvertTo-Html

显然你只需要把它插入你现有的循环......但我会把它留在你干练的手中:-)

结果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/
xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/></colgroup>
<tr><th>Source</th><th>Dest1</th><th>Dest2</th><th>Dest3</th></tr>
<tr><td>File 1</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
<tr><td>File 2</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
<tr><td>File 3</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
</table>
</body></html>

-Fragment

$results | Select-Object Source, Dest1, Dest2, Dest3 | ConvertTo-Html -Fragment

<table>
<colgroup><col/><col/><col/><col/></colgroup>
<tr><th>Source</th><th>Dest1</th><th>Dest2</th><th>Dest3</th></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
</table>

不是PowerShell真棒吗?!