Share via


splitting a multiline String object for HTML

Question

Thursday, April 21, 2016 12:09 PM

In my script I list all members of the of Remote Desktop Users with net localgroup command, assign it to a variable and pipe to Out-String

Problem is when I use ConvertTo-HTML instead of a list of members I get all member names separated by space. How to format it into a list with a break line after each member?

yaro

All replies (8)

Friday, April 22, 2016 10:00 AM ✅Answered

Hi Yaro,

Thanks for the post, you can test the below code and check if it works for you:

$server = $env:COMPUTERNAME
$localgroup = "Remote Desktop Users"
$Group= [ADSI]"WinNT://$Server/$LocalGroup,group"
$members = $Group.psbase.Invoke("Members")
$a= $members | ForEach-Object { $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
$users= @()
$p= $a.count
For($i= 0; $i -lt $p; $i++){
$user= New-object -type PSobject 
$user| ADD-MEMBER  -MemberType NoteProperty -Name User -Value $a[$i]
$users+= $user
}

$USERS |ConvertTo-Html -Title "remote desktop users"  -Property User | Out-file C:\Remotedesktopuser.html

I've tested it on my lab and it works.

Best Regards,

Elaine

Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected].


Friday, April 22, 2016 6:11 PM ✅Answered

Here is how to inject plain text into HTML:

PS > $body=net localgroup 'Remote Desktop Users'|%{$_+'<br/>'}
PS > ConvertTo-Html -Body $body|Out-File test.htm
PS > .\test.htm
PS >

\(ツ)_/


Thursday, April 21, 2016 12:43 PM | 1 vote

You cannot convert a string into HTML.

Add "<br />" to the end of each line.

\(ツ)_/


Thursday, April 21, 2016 12:54 PM

Would it be possible to split the string before converting then or would it end in creating a separate cell in a table for each line converted to separate object?

yaro


Thursday, April 21, 2016 12:56 PM

ConvertTo=Html works on objects and not on strings.

\(ツ)_/


Friday, April 22, 2016 5:35 PM

Thanks Elaine. This indeed splits the users into separate lines but it causes the end html file to hold each line in a separate cell if you apply formatting. I was actually thinking of getting the members separated to have a nice list in a single cell where next cell would list say local Admins and so on. So far I only managed to separate the members with ; so at least they don't completely mix up together. 

yaro


Friday, April 22, 2016 7:19 PM

Excuse my lack of knowledge but how would I make the above a fragment of bigger html document as for the moment I have a couple of fragments prepared with -Fragment -Precontent yet with the -Body parameter I don't get the -Fragment switch as an option anymore.

yaro


Friday, April 22, 2016 7:36 PM

Excuse my lack of knowledge but how would I make the above a fragment of bigger html document as for the moment I have a couple of fragments prepared with -Fragment -Precontent yet with the -Body parameter I don't get the -Fragment switch as an option anymore.

yaro

Reading the help and searching online will show you how to use PowerShell CmdLets.

$frag1=...
$frag2=...
ConvertTo-Html -Body $frag1,$frag2 ....

\(ツ)_/