Solved

How to convert an array to a string and use it in ConvertTo-HTML

Posted on 2013-06-25
21
2,771 Views
Last Modified: 2013-06-30
I have a file I read in that I want to convert to a string then send it to ConvertTo-HTML.  I'm sure this is real easy but everything I try fails.  I need some help

This is my code

$memory = get-content "c:\bat\logs\Results.txt" | out-string
write-host $memory
$memoryList = $memory | ConvertTo-HTML -Fragment -PreContent "<h2>Memory Usage</h2>" | Out-String
write-Host $memoryList

When I run it this is my result

$memory = get-content "c:\bat\logs\Results.txt" | out-string
$memoryList = $memory | ConvertTo-HTML -Fragment -PreContent "<h2>Memory Usage</h2>" | Out-String
write-Host $memoryList
<h2>Memory Usage</h2>
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>112</td></tr>
</table>

The values in the file are:
38.47% 42.31% 38.03% 33.91% 27.44% 22.43% 22.46% 22.51% 20.66% 20.85% 19.89% 16.86% 17.01% 18.20%


Thanks for the help
0
Comment
Question by:c7c4c7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 3
  • +1
21 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277163
$memory = get-content "c:\temp\1.txt" | out-string
$memoryList = $memory -split ' ' | Select-Object @{Name='String';Expression={$_}} | select String | ConvertTo-HTML -Fragment -PreContent "<h2>Memory Usage</h2>" | Out-String
write-Host $memoryList

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39277658
You can also try this..
(GC "c:\bat\logs\Results.txt") -split " " | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage"

Open in new window

0
 

Author Comment

by:c7c4c7
ID: 39278875
Sedgwick Couple of questions
How can I add additional spaces to the resulting string so it is more readable?
The output from your answer produces an * and then the report I want see, where did the * come from?

The output is attached
HTML-output.txt
0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:c7c4c7
ID: 39280276
Can anyone help with my last post
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39280286
Yes. Ill be in the office in an hour and ill post u back.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39280362
cls
$memoryList = (gc "c:\bat\logs\Results.txt") -split ' ' | Select-Object @{Name='Value';Expression={$_}} | select  " ", Value | ConvertTo-HTML -Fragment -PreContent "<h2>Memory Usage</h2>" | Out-String
write-Host $memoryList

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39280368
@c7c4c7, Did you try my code?
0
 

Author Comment

by:c7c4c7
ID: 39283046
Sedgwick
thanks for the update
that got rid of the * but now its a verticle list rather than the horizontal one in the original code, I would rather have it in the original format so that it does not take up so much space in the report.  

Can it be in the original format with an addition space between the individual values and without the *?

subsun, yes I tried your code.  it produced a report that had an * and a verticle column as well.  I am new to powershell, I'm sure you guessed that, and Sedwick is easier to understand
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39283117
You can always ask for explanation, that's how you learn it..:-)

BTB my code wont add * to output html.. as we are using ConvertTo-Html "Memory Usage",by using this ConvertTo-Html will save specific property in HTML. (-Property is a positional parameter so we don;t have to specify ConvertTo-Html -Property "Memory Usage")
HTMI have seen, This is applicable only for objects which has single property..
For example..
Following command will add * to the HTML output..
Get-Service | Select name | Convertto-html | Out-File test.htm

But following commands wont..
Get-Service | Select name | Convertto-html name  | Out-File test.htm # we specify property for Convertto-html
Get-Service | Select name,Status | Convertto-html | Out-File test.htm # the input object has more than one property name & Status

Open in new window


I have modified the code like yours..
$memory = Get-Content "c:\bat\logs\Results.txt"
write-host $memory
$memoryList = $memory -split " " | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage"
write-Host $memoryList

Open in new window

HTH
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39283576
Subsun,
This will still display a real table, instead of space separated list of values. If we omit the splitting, that should do:
$memory = Get-Content "c:\bat\logs\Results.txt"
write-host $memory
$memoryList = $memory | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage"
write-Host $memoryList

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39283935
You are right.. I have completely missed the 'horizontal' part from the comment.. Thanks for catching that... :-)

This will add some space between the values..
$memory = Get-Content "c:\bat\logs\Results.txt"
write-host $memory
$memoryList = ($memory | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage" ) -replace  '%','% &nbsp;&nbsp; '
Write-host $memoryList

Open in new window

0
 

Author Comment

by:c7c4c7
ID: 39285023
Subsun


I added your code to mine and received the following error when running it

ConvertTo-Html : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'PostContent'.
Specified method is not supported.
At C:\Powershell\just_countsV1.ps1:23 char:17
+     ConvertTo-HTML @params | Out-File -FilePath $filepath
+                    ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-Html], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertToHtmlCommand

When I modified your code to
      $memory = Get-Content "c:\bat\logs\Results.txt"
      $memoryList = ($memory | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage" ) -replace  '%','% &nbsp;&nbsp; ' | Out-String

It produced the report in a verticle format not horizontal

I've included a copy of the chopped down code so you can see more of what is going on
0
 

Author Comment

by:c7c4c7
ID: 39285027
Here's the code
just-countsV2.txt
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39285112
This is what I get when I run your code..
HTML Output
0
 

Author Comment

by:c7c4c7
ID: 39286250
I'm not sure what to say.
I've attached my output
events.html
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39286455
Could you post c:\bat\logs\Results.txt, too?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39286628
From your code remove the following line and try..
$style = "<style>$(get-content C:\Powershell\CSS\style.css)</style>"

If it's not working then, just run following code and see are you getting the vertical result?
$memory = Get-Content "c:\bat\logs\Results.txt"
write-host $memory
$memoryList = ($memory | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage" ) -replace  '%','% &nbsp;&nbsp; '
$memoryList | Out-File test.htm

Open in new window


I've included a copy of the chopped down code so you can see more of what is going on
in addition to Results.txt (Qlemo's request).. post the style.css and the complete script (from above comment I think you have not posted the complete code..)..
0
 

Author Comment

by:c7c4c7
ID: 39287621
The code that I posted is the complete code.  I removed a bunch of code from the original version just to make it easier to work with.  The code posted everything that is producing the error.

I removed the style sheet and received the same type of data formatted vertically

I've included the style sheet the updated v2, with the style sheet commented out as well as the results file

I've also added a text file of the html produced, can you upload yours so I can see the difference
Results.txt
events.html
style.css
just-countsV2.txt
Daily-Report-for---06-29-2013-18.txt
0
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 39287809
Oh boy!.. in your Results.txt file the values are vertical...

Change line $memory = get-content "c:\bat\logs\Results.txt"

to
$memory = (get-content "c:\bat\logs\Results.txt") -join " "

Open in new window


Try..
#	$computerName = get-content env:computername
	$date = get-date
	
	$filepath = "c:\bat\logs\events.html"
	$path = "C:\bat\servers.txt"
	$computers = gc $path

	$style = "<style>$(get-content C:\Powershell\CSS\style.css)</style>"

        $memory = (get-content "c:\bat\logs\Results.txt") -join " "
	$memoryList = ($memory | Select @{N="Memory Usage";E={$_.Trim()}} | ConvertTo-Html "Memory Usage" ) -replace  '%','% &nbsp;&nbsp; ' | Out-String
	
	#Get Count of System Errors last 24 hrs
	$events = Get-Eventlog -logname system -EntryType Error -After ((Get-Date).Date.AddDays(-1))
	$system_errors = $events | Group-Object -property source -noelement | Sort-Object -property count -descending | select Count,Name | ConvertTo-HTML -Fragment -PreContent "<h2>System Errors</h2>"| Out-String
	

	#Format it to HTML and sace it in c:\bat\logs\events.html
	$params = @{'Head'="<title>Daily Report for $computer - $date</title>$style";
		'PreContent'="<h1>System Event Report for $computerName  - $date</h1>";
		'PostContent'= $memoryList, $system_errors}
	ConvertTo-HTML @params | Out-File -FilePath $filepath

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39287981
"in your Results.txt file the values are vertical..."
That is what I have feared ...
0
 

Author Closing Comment

by:c7c4c7
ID: 39288273
Great!, Excelent work

I really appreciate your sticking with me on this.  I'm afraid it would have taken me a really long time to figure this out on my own
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question