1

I have the string value that has 
. I want replace this with \n. and when I convert it to html format after each line comes to another line. But this not work.

$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"

After using Convert-html I have the text like this :

'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

'

How can I do this?

1
  • 1
    \ is not a character escape sequence in PowerShell, you should use a backtick instead: "`n" Commented Jun 27, 2015 at 16:07

3 Answers 3

2

The approach Mathias mentions works fine for newlines. But if you have (or could have) other entity refs then I would use the HtmlDecode method e.g.

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

Outputs:

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Try this

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

Gives me

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.