在powershell数组中换行然后将其转换为html? [英] Break lines in powershell array and then convert it to html?

查看:41
本文介绍了在powershell数组中换行然后将其转换为html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发誓我已经搜索了很多,但仍然没有找到任何解决方案.我所做的基本上是将数组转换为 HTML,但我找不到任何方法在新行中打破数组的单词.

例如:

$Member.ExpireOn=((Invoke-SqliteQuery -SQLiteConnection $C -Query "SELECT expireon FROM exceptions_test WHERE (identity='$User')").ExpireOn -join "`r`n")

$Member.ExpireOn 基本上是这样的:

31/01/2019 00:00:00 31/03/2019 00:00:00 31/03/2019 00:00:00

所以即使我用 -join "rn"
加入它,我也找不到任何方法为数组中的每个元素设置一行.我需要这样做,因为之后我将整个数组打印在一个 HTML 文件中,这就是我得到的:

而不是在这些单词之间使用
.希望有人能帮助我:)

解决方案

即使我用 -join "`r`n"

HTML 中的空白是无关紧要的,因此您不能以这种方式强制换行;它们将显示在 HTML 标记(源代码)中,但不会显示在 呈现时.

<块引用>

或使用

问题在于

<小时>

如果您需要更强大的解决方案,则必须使用 HTML 解析器 - 请参阅这个答案.

I swear I've been searching a lot and still haven't found any solution to this thing. What I do is basically converting arrays to HTML but I can't find any way to break the words of the array in new lines.

For example:

$Member.ExpireOn=((Invoke-SqliteQuery -SQLiteConnection $C -Query "SELECT expireon FROM exceptions_test WHERE (identity='$User')").ExpireOn -join "`r`n")

$Member.ExpireOn is basically this:

31/01/2019 00:00:00 31/03/2019 00:00:00 31/03/2019 00:00:00

so even if I join it with -join "rn" or with <br> I can't find any way to have a line for each element in the array. I need to do it because after that I print the whole array in a HTML file and this is what I get:

instead of having a <br> between those words. Hopefully somebody can help me :)

解决方案

even if I join it with -join "`r`n"

Whitespace in HTML is insignificant, so you cannot force line breaks this way; they will show in the HTML markup (source code), but not when rendered.

or with <br>

The problem is that ConvertTo-Html - which I assume you're using - escapes any HTML markup in the input objects' property values (it assumes you want to use the values verbatim), so you cannot pass <br> through in order to make a single table cell use multiple line - you'll see literal <br> strings in the table, because ConvertTo-Html has escaped them as &lt;br&gt;.


A quick and dirty workaround would be to manually convert the escaped <br> elements back to their HTML form:

# Sample input objects
$o = [pscustomobject] @{
  Foo = 'Cert A'
  # Join the array elements with <br>
  ExpireOn = (Get-Date), (Get-date).AddDays(1) -join '<br>'
}, [pscustomobject] @{
  Foo = 'Cert B'
  ExpireOn = (Get-Date).AddDays(2), (Get-date).AddDays(3) -join '<br>'
}

# Convert *escaped* <br> elements back to literal '<br>'
# NOTE: This will replace *all* instances of '&lt;br&gt;' in the 
#       document text, wherever it may occur.
($o | ConvertTo-Html) -replace '&lt;br&gt;', '<br>'

This yields the following, showing that the <br> was effectively passed through, which should make the input date values render on individual lines:

<!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/></colgroup>
<tr><th>Foo</th><th>ExpireOn</th></tr>
<!-- Note the <br> elements -->
<tr><td>Cert A</td><td>2/5/2020 12:51:45 PM<br>2/6/2020 12:51:45 PM</td></tr>
<tr><td>Cert B</td><td>2/7/2020 12:51:45 PM<br>2/8/2020 12:51:45 PM</td></tr>
</table>
</body></html>

In Chrome on my Mac, this renders as follows:


If you need a more robust solution, you'll have to use an HTML parser - see this answer.

这篇关于在powershell数组中换行然后将其转换为html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆