Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3248
  • Last Modified:

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

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
c7c4c7
Asked:
c7c4c7
  • 8
  • 7
  • 3
  • +1
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
$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
 
SubsunCommented:
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
 
c7c4c7Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
c7c4c7Author Commented:
Can anyone help with my last post
0
 
Meir RivkinFull stack Software EngineerCommented:
Yes. Ill be in the office in an hour and ill post u back.
0
 
Meir RivkinFull stack Software EngineerCommented:
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
 
SubsunCommented:
@c7c4c7, Did you try my code?
0
 
c7c4c7Author Commented:
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
 
SubsunCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
SubsunCommented:
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
 
c7c4c7Author Commented:
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
 
c7c4c7Author Commented:
Here's the code
just-countsV2.txt
0
 
SubsunCommented:
This is what I get when I run your code..
HTML Output
0
 
c7c4c7Author Commented:
I'm not sure what to say.
I've attached my output
events.html
0
 
QlemoC++ DeveloperCommented:
Could you post c:\bat\logs\Results.txt, too?
0
 
SubsunCommented:
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
 
c7c4c7Author Commented:
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
 
SubsunCommented:
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
 
QlemoC++ DeveloperCommented:
"in your Results.txt file the values are vertical..."
That is what I have feared ...
0
 
c7c4c7Author Commented:
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

Industry Leaders: 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!

  • 8
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now