PowerShell - 将变量传递给HTML输出 - 单独的行

时间:2016-03-04 00:04:41

标签: html powershell

我有一个搜索脚本来查找某些网络/本地位置的文件。完整的脚本工作正常,并返回正确的结果。我只是清理html输出结果文件,以便其他人可以轻松阅读。

请注意: $ Paths = Get-Content H:\ location.txt 返回两个值h:\ test和c:\ test

在我的PowerShell脚本中,当我将$ paths变量传递给我的HTML输出时,我得到了正确的信息,但它显示在一行中,即h:\ test c:\ test但是

我希望输出格式更像下面的屏幕:

h:\ test
C:\测试

$SubFolders = $true
$ReportPath = "h:\"
$Paths = Get-Content H:\location.txt
$text = ('*md5*','*build*')

foreach ($path in $paths) {

If ($SubFolders)
{   $SubFoldersText = "(including sub-folders)"
}
Else
{   $SubFoldersText = "(does <i>not</i> include sub-folders)"
}

$Header = @"

<style type='text/css'>
body { background-color:#DCDCDC;
}
table { border:1px solid gray;
  font:normal 12px verdana, arial, helvetica, sans-serif;
  border-collapse: collapse;
  padding-left:30px;
  padding-right:30px;
}
th { color:black;
  text-align:left;
  border: 1px solid black;
  font:normal 16px verdana, arial, helvetica, sans-serif;
  font-weight:bold;
  background-color: #FF0000;
  padding-left:6px;
  padding-right:6px;
}
td { border: 1px solid black;
  padding-left:6px;
  padding-right:6px;
}
</style>
<center>
<h1>Software Audit</h1>
<h2>Locations that are being searched: <font color="red"> <br/> $paths </font> 
<br/> Searching for files that contain the words: <font color="red"> <br/> $text </font> <br/> 
$SubFoldersText</h2>
<br>
"@

If ($FileExtension -eq "*")
{   $GCIProperties = @{
    Path = $Paths
    Recurse = $SubFolders
}
}
Else
{   $GCIProperties = @{
    Path = $Paths
    Include = $text
    Recurse = $SubFolders

}
}

$Report = @()
Foreach ($File in (Get-ChildItem @GCIProperties | Where { $_.PSisContainer -    eq $false }))
{
$Report += New-Object PSObject -Property @{
    Path = $File.FullName
    Owner = (Get-Acl $File.FullName).Owner
    'Created on' = $File.CreationTime
    'Last Write Time' = $File.LastWriteTime
    Size = "{0:N2} MB" -f ( $File.Length / 1mb )
}
}

}

$Report | Select Path,Size,'Created on','Last Write Time',Owner | Sort Path     | ConvertTo-Html -Head $Header | Out-File $ReportPath\Get-FileOwner.html

Invoke-Item $ReportPath\Get-FileOwner.html

感谢您的帮助,

2 个答案:

答案 0 :(得分:1)

Get-Content后使用$paths -join "<br/>"

$paths = Get-Content H:\location.txt
$paths = $paths -join "<br/>"
...

答案 1 :(得分:1)

格式化为HTML时,将$paths更改为$($paths -join '<br/>')

<h2>Locations that are being searched: <font color="red"> <br/> $($paths -join '<br/>') </font>

字符串中的$()语法允许您放置任何PowerShell表达式。在这种情况下,字符串连接。