使用Powershell以html格式插入换行符

时间:2015-06-27 09:39:50

标签: powershell powershell-v3.0

我的字符串值为
。我想用\n替换它。当我将每行转换为另一行后将其转换为html格式。但这不起作用。

$StdOut = 'total 40
drwxr-xr-x 3 root root 4096 Jun 16 14:55 .
drwxr-xr-x 5 root root 4096 Jun 16 14:54 ..
-rw------- 1 root root    0 Jun 16 14:55 cimserver_start.lock
srwxrwxrwx 1 root root    0 

Jun 16 14:55 cim.socket drwxr-xr-x 2 root root 4096 Jun 16 17:58 localauth -rw ------- 1 root root 6 Jun 16 14:55 scx-cimd.pid '

$CResult = $StdOut -replace "
", "\n"

使用Convert-html后,我有这样的文字:

'total 40\ndrwxr-xr-x 3 root root 4096 Jun 16 14:55 .\ndrwxr-xr-x 5 root root 4096 Jun 16 14:54 ..\n-rw------- 1 root root    0 Jun 16 14:55 cimserver_start.lock\nsrwxrwxrwx 1 root root    0 Jun 16 14:55
 cim.socket\ndrwxr-xr-x 2 root root 4096 Jun 16 17:58 localauth\n-rw------- 1 root root    6 Jun 16 14:55 scx-cimd.pid\n

'

我该怎么做?

3 个答案:

答案 0 :(得分:2)

Mathias提到的方法适用于换行。但是如果你有(或可能有)其他实体引用,那么我会使用HtmlDecode方法,例如

Add-Assembly System.Web
[System.Web.HttpUtility]::HtmlDecode($StdOut)

输出:

total 40
drwxr-xr-x 3 root root 4096 Jun 16 14:55 .
drwxr-xr-x 5 root root 4096 Jun 16 14:54 ..
-rw------- 1 root root    0 Jun 16 14:55 cimserver_start.lock
srwxrwxrwx 1 root root    0

答案 1 :(得分:0)

PS C:\WINDOWS\system32> Add-Assembly System.Web
Add-Assembly : The term 'Add-Assembly' is not recognized as the name of a   
cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if 

a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Add-Assembly System.Web
+ ~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Add-Assembly:String) [],   
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

答案 2 :(得分:0)

试试这个

$null = [Reflection.Assembly]::LoadWithPartialName('System.Web')
[System.Web.HttpUtility]::HtmlDecode($StdOut)

给我

total 40
drwxr-xr-x 3 root root 4096 Jun 16 14:55 .
drwxr-xr-x 5 root root 4096 Jun 16 14:54 ..
-rw------- 1 root root    0 Jun 16 14:55 cimserver_start.lock
srwxrwxrwx 1 root root    0