GettingStarted
GettingStarted

Reputation: 7625

How to read each element from a TextFile

Server_Name                                 Process_Name                               Server_Status                              Process_Available                         
-----------                                 ------------                               -------------                              -----------------                         
FILESTORAGE1                                  notepad                                    Online                                     No                                        
FILESTORAGE1                                  explorer                                   Online                                     Yes                                       
FILESTORAGE1                                  Sampler                                    Online                                     No                                        
FILESTORAGE1                                notepad                                    Online                                     No                                        
FILESTORAGE1                                explorer                                   Online                                     Yes                                       
FILESTORAGE1                                Sampler                                    Online                                     Yes                                       
FILESTORAGE1                                     notepad                                    Offline                                    No                                        
FILESTORAGE1                                     explorer                                   Offline                                    No                                        
FILESTORAGE1                                     Sampler                                    Offline                                    No                                        

My file looks like this.

I want to get each element from my file and create an HTML file (from this TXT)

 $bodyFromFile = Get-Content "$PSScriptRoot\data.txt"
 $bodyString = $bodyFromFile | ConvertTo-Html

Unfortunately the contents of bodyString looks like it's legitimate HTML. The data from the file isn't in the variable.

Upvotes: 0

Views: 63

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200483

Don't convert your objects to a string when you want the end result to be tabular HTML output. Instead of

... | Out-String | Out-File "$PSScriptRoot\data.txt"
$bodyFromFile = Get-Content "$PSScriptRoot\data.txt"
$bodyString = $bodyFromFile | ConvertTo-Html

do something like this:

$table = ... | ConvertTo-Html -Fragment
$bodyString = "<body>$table</body>"

Upvotes: 1

Related Questions