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

x
?
Solved

Read a text file and generate  html one having tabular format in powershell using regex and pattern matching

Posted on 2016-11-06
4
Medium Priority
?
34 Views
Last Modified: 2016-11-27
Below mentioned is  a text of text file

ServeName is STAETL1003Q

DomainName is INFA_DOMAIN_STAETL1003Q

PortName is 6500

ServeName is STAETL1003Qa

DomainName is INFA_DOMAIN_STAETL1003Qa

PortName is 6500

ServeName is STAETL1003Qb

DomainName is INFA_DOMAIN_STAETL1003Qb

PortName is 6501
0
Comment
Question by:Tanu Sidana
  • 3
4 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41876671
$TxtFile = 'C:\Temp\temp.txt'
$HtmlFile = 'C:\Temp\temp.html'
$Properties = [ordered]@{}
Get-Content $TxtFile | ForEach-Object {
	If ($_ -match '\A(?<Key>\S+)\s+is\s+(?<Value>.+)\Z') {
		$Properties[$Matches['Key'].Trim()] = $Matches['Value'].Trim()
		If ($Matches['Key'] -eq 'PortName') {
			New-Object -TypeName PSObject -Property $Properties
			$Properties.Clear()
		}
	}
} | ConvertTo-Html | Set-Content -Path $HtmlFile

Open in new window

0
 

Author Comment

by:Tanu Sidana
ID: 41876745
Can you please explain me the logic and i need table borders too
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points (awarded by participants)
ID: 41876756
The script reads the content of the input file, checks for lines matching "<Key> is <Value>".
The results will be stored in a hashtable until a key named "PortName" (the last element in a block) is found, and then written to the pipeline, from where they will be picked up by ConvertTo-Html and finally Set-Content.
$TxtFile = 'C:\PS\temp.txt'
$HtmlFile = 'C:\PS\temp.html'
$HtmlHead = @'
<style>
	TABLE{border-width:1px; border-style:solid; border-color:black;}
	TH{border-width:1px; padding:1px; border-style:solid; border-color:black;}
	TD{border-width:1px; padding:1px; border-style:solid; border-color:black;}
</style>
'@

$Properties = [ordered]@{}
Get-Content $TxtFile | ForEach-Object {
	If ($_ -match '\A(?<Key>\S+)\s+is\s+(?<Value>.+)\Z') {
		$Properties[$Matches['Key'].Trim()] = $Matches['Value'].Trim()
		If ($Matches['Key'] -eq 'PortName') {
			New-Object -TypeName PSObject -Property $Properties
			$Properties.Clear()
		}
	}
} | ConvertTo-Html -Head $HtmlHead | Set-Content -Path $HtmlFile

Open in new window

0
 
LVL 85

Expert Comment

by:oBdA
ID: 41902956
Question answered.
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!

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Loops Section Overview

963 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