简体   繁体   English

Powershell将结果放在表中

[英]Powershell out put results in a table

I am copying files from a source to multiple locations and then do a check against source to see if files are copied successfully. 我将文件从一个源复制到多个位置,然后对源进行检查以查看文件是否成功复制。 $result will list the results in console line by line, but when I trying to send $result to email, it ends up with everything in one line. $result将在控制台中逐行列出结果,但是当我尝试将$result发送到电子邮件时,它最终将所有内容都显示在一行中。 I tried format-table and converto-htl without success. 我尝试了format-tableconverto-htl但没有成功。 I would like to use hashtable but need some guidance. 我想使用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

Here is what I want to have in final look. 这是我最后要看的内容。

在此处输入图片说明

Code

# 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

Obviously you just need to bung this in to your existing loop... but I'll leave that in your capable hands :-) 显然,您只需要将其绑定到您现有的循环中即可...但是我会把它留在您有能力的手中:-)

Results 结果

<!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>

Isn't PowerShell awesome?! PowerShell很棒吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM