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

标签 html powershell

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

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

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

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

h:\ test
c:\ test

$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

谢谢你的帮助,

最佳答案

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

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

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

关于html - PowerShell-将变量传递到HTML输出-单独的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35785550/

相关文章:

javascript - CSS Bootstrap 通过 javascript 创建元素

html - 没有特定类后代的所有元素的 XPath?

powershell - 如何让 powershell 函数返回一个对象并设置 $? $假?

powershell - 有没有办法使用 Powershell 操作内存中的 zip 文件内容?

powershell - 无法加载 WITDataStore64 dll

html - 按百分比设置填充时垂直菜单转到新行

html - 滚动时保持在顶部的凹形标题

php - 在 html 中嵌入 php 以在用户提交值后将文本保留在文本框中

shell - 可以将命令别名自动添加到DOS shell中吗?

windows - Powershell脚本自动输入端口号