Solved

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

Posted on 2013-06-25
21
2,487 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
  • 8
  • 7
  • 3
  • +1
21 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
$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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:c7c4c7
Comment Utility
Can anyone help with my last post
0
 
LVL 42

Expert Comment

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

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
@c7c4c7, Did you try my code?
0
 

Author Comment

by:c7c4c7
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
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
Comment Utility
Here's the code
just-countsV2.txt
0
 
LVL 40

Expert Comment

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

Author Comment

by:c7c4c7
Comment Utility
I'm not sure what to say.
I've attached my output
events.html
0
 
LVL 68

Expert Comment

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

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
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
Comment Utility
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 68

Expert Comment

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

Author Closing Comment

by:c7c4c7
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now