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

x
?
Solved

extract parts of a file with powershell

Posted on 2016-08-23
12
Medium Priority
?
65 Views
Last Modified: 2016-08-24
good evening

I need to extract parts of a .txt file

I wonder if you can do this with powershell.

The file that I need to read is attached, I need only extract this information as follows:


Server Blade #1 Information:
        Product Name: ProLiant BL460c Gen8
        Serial Number: BRC31939CV
        Server Name: EBR001001-311
        CPU 1:  Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz (8 cores)
        Memory: 262144 MB

#1           EBR001001-311                 ProLiant BL460c Gen8   BRC31939CV      Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz (8 cores)   262144 MB


thank you
EHP1.txt
0
Comment
Question by:Support_38
  • 8
  • 3
12 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 41767959
It can be done, but your question is a little under-defined for actually writing a script.

Is your goal to output only the line with the extracted field information, in that order? Did you want to label the columns of the output? Do you want to output tab-delimited fields. Are you looking for the very first entry in the file or are you looking for a particular name. Does it make sense to convert the entire input file to an object collection that can be used elsewhere in PowerShell, or do you just want unlabeled items in an array?
0
 

Author Comment

by:Support_38
ID: 41768080
Hello

 I need to bring dynamic information , these lines are fixed , so just need to bring the information to a text file as it is. shown it. bring line 1, line 5 ...
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41768232
This writes the results to the pipeline as custom objects with the properties you want. You can pipe the output through whatever you want, like Format-Table, Format-List, Export-Csv, ...
$InputFile = 'C:\Temp\EHP1.txt'

Switch -regex -File $InputFile {
	'\AServer Blade #(?<Blade>\d+) Information:\Z' {
		$Result = '' | Select-Object -Property 'Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory'
		$Result.Blade = $Matches['Blade']
	}
	'\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
	'\A\s+Product Name:\s*(?<ProductName>.*?)\s*\Z' {$Result.ProductName = $Matches['ProductName']}
	'\A\s+Serial Number:\s*(?<SerialNumber>\S+)\s*\Z' {$Result.SerialNumber = $Matches['SerialNumber']}
	'\A\s+CPU 1:\s*(?<CPU>.*?)\s*\Z' {$Result.CPU = $Matches['CPU']}
	'\A\s+Memory:\s*(?<Memory>\d+)\s+.*\Z' {
		$Result.Memory = $Matches['Memory']
		$Result
	}
}

Open in new window

1
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Support_38
ID: 41768522
Good Morning,
Perfect, the result was the same, very good.

If I need the information in columns, you can do?

So I have both formats.

I appreciate the help
0
 

Author Comment

by:Support_38
ID: 41768526
This format is possible?

Blade      Servername           ProductName              SerialNumber                        CPU             Memoria                              
  1       EBR001001-011       Prolianr BL460c             BRC510671T      Intel(R) Xeon(R) CPU...    327680
  2       EEC002002-101       ProLiant BL460c Gen8        BRC510671K      Intel(R) Xeon(R) ...       262144
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41768559
As I said: you can use anything that PS offers in terms of formatting, or you can use Export-Csv to process that further in Excel or wherever.
For a formatted table, use Format-Table; this should fit into a standard 120 character wide PS console:
.\Whatever.ps1 | Format-Table -AutoSize

Open in new window

0
 

Author Comment

by:Support_38
ID: 41768611
Thank you.
0
 

Author Closing Comment

by:Support_38
ID: 41768616
Thank you.
0
 

Author Comment

by:Support_38
ID: 41768740
A final information.

I need to read multiple files automatically in the same directory and generate a single output?

I have 8 files:

EHP1.txt
EHP2.txt
EHP3.txt
...

It's possible ?
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41768756
Now has an additional column "File" with the file name from which the row came from.
$InputFile = 'C:\Temp\EHP{0}.txt'
1..8 | ForEach-Object {
	$FileName = $InputFile -f $_
	Switch -regex -File $FileName {
		'\AServer Blade #(?<Blade>\d+) Information:\Z' {
			$Result = '' | Select-Object -Property 'File', 'Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory'
			$Result.File = [IO.Path]::GetFileName($FileName)
			$Result.Blade = $Matches['Blade']
		}
		'\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
		'\A\s+Product Name:\s*(?<ProductName>.*?)\s*\Z' {$Result.ProductName = $Matches['ProductName']}
		'\A\s+Serial Number:\s*(?<SerialNumber>\S+)\s*\Z' {$Result.SerialNumber = $Matches['SerialNumber']}
		'\A\s+CPU 1:\s*(?<CPU>.*?)\s*\Z' {$Result.CPU = $Matches['CPU']}
		'\A\s+Memory:\s*(?<Memory>\d+)\s+.*\Z' {
			$Result.Memory = $Matches['Memory']
			$Result
		}
	}
}

Open in new window

0
 

Author Comment

by:Support_38
ID: 41768785
Very good.
0
 

Author Comment

by:Support_38
ID: 41768887
Hi

If I need to get information from another block (Management Processor Information:) ex, IP Address, how should I mount the script?
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
Loops Section Overview

886 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