gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:13:00 25 4
gpt4 key购买 nike

我有一个搜索脚本,可以在某些网络/本地位置中查找文件。完整的脚本可以正常工作,并返回正确的结果。我只是清理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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com