Solved

extract parts of a file with powershell

Posted on 2016-08-24
14
41 Views
Last Modified: 2016-08-26
Good evening,

I opened a question yesterday and was promptly answered with my need.

After I close the matter, they asked me to include two more fields in the output file and I do not know much about powershell or regular expression.

The script that solved my problem is this:

$ InputFile = 'C: \ Temp \ EHP {0} .txt'
1..8 | ForEach-Object {
$ FileName = $ InputFile -f $ _
Switch -regex -File $ FileName {
'\ ASERVER Blade # Information (<Blade> \ d +?): \ 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 * \ s * \ Z (<ServerName> \ S +?)' {$ Result.ServerName = $ Matches [ 'ServerName']}
'\ A \ s + Product Name: (.? <ProductName> *) \ s * \ s * \ Z' {$ Result.ProductName = $ Matches [ 'ProductName']}
'\ A \ s + Serial Number: (<SerialNumber> \ S +) \ s * \ s * \ Z' {$ Result.SerialNumber = $ Matches [ 'SerialNumber']}
'\ A \ s + 1 CPU: (.? <CPU> *) \ s * \ s * \ Z' {$ Result.CPU = $ Matches [ 'CPU']}
'\ A \ s + Memory: \ s * \ s + * \ Z (<Memory> \ d +?).' {
$ Result.Memory = $ Matches [ 'Memory']
$ Result
}
}
}


But I need to include IP Address: and SystemBoardSparePartNumber:

I tried to adapt the script, but does not work.

Attached the file where the information is extracted.

Again thank you for help
EHP1.txt
0
Comment
Question by:Alex
  • 7
  • 5
  • 2
14 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
I am going to look at this more deeply, but if anyone else gets to it first, the original question can be found at:
https://www.experts-exchange.com/questions/28965187/extract-parts-of-a-file-with-powershell.html
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
$InputFile = 'C: \ Temp \ EHP {0} .txt'
1..8 | ForEach-Object {
$FileName = $InputFile -f $_
Switch -regex -File $FileName {
'\ ASERVER Blade # Information (<Blade> \ d +?): \ Z' {
$Result = '' | Select-Object -Property 'File', 'Blade', 'ServerName', 'ProductName', 'SystemBoardSparePartNumber', 'SerialNumber', 'CPU', 'Memory'
$Result.File = [IO.Path] :: GetFileName ($FileName)
$Result.Blade = $Matches [ 'Blade']
}
'\ A \ s + Server Name: \ s * \ s * \ Z (<ServerName> \ S +?)' {$Result.ServerName = $Matches [ 'ServerName']}
'\ A \ s + Product Name: (.? <ProductName> *) \ s * \ s * \ Z' {$Result.ProductName = $Matches [ 'ProductName']}
'\ A \ s + System Board Spare Part Number: (.? <SystemBoardSparePartNumber> *) \ s * \ s * \ Z' {$Result.SystemBoardSparePartNumber = $Matches [ 'SystemBoardSparePartNumber']}
'\ A \ s + Serial Number: (<SerialNumber> \ S +) \ s * \ s * \ Z' {$Result.SerialNumber = $Matches [ 'SerialNumber']}
'\ A \ s + 1 CPU: (.? <CPU> *) \ s * \ s * \ Z' {$Result.CPU = $Matches [ 'CPU']}
'\ A \ s + Memory: \ s * \ s + * \ Z (<Memory> \ d +?).' {
$Result.Memory = $Matches [ 'Memory']
$Result
}
}
}

Open in new window


Can you try this and let me know if you get the system board spare part information?
I need to get out of here at the moment as it is late but I will look into the IP piece in a bit.
0
 

Author Comment

by:Alex
Comment Utility
Hello,
I run the script but showing error as follows:



No linha:7 caractere:26
+ $Result.File = [IO.Path] :: GetFileName ($FileName)
+                          ~~
Token '::' inesperado na expressão ou instrução.
No linha:8 caractere:26
+ $Result.Blade = $Matches [ 'Blade']
+                          ~
Token '[' inesperado na expressão ou instrução.
No linha:8 caractere:27
+ $Result.Blade = $Matches [ 'Blade']
+                           ~
Nome de tipo ausente depois de '['.
No linha:10 caractere:95
+ ... ame = $Matches [ 'ServerName']}
+                    ~
Token '[' inesperado na expressão ou instrução.
No linha:10 caractere:96
+ ... me = $Matches [ 'ServerName']}
+                    ~
Nome de tipo ausente depois de '['.
No linha:11 caractere:96
+ ... ame = $Matches [ 'ProductName']}
+                    ~
Token '[' inesperado na expressão ou instrução.
No linha:11 caractere:97
+ ... me = $Matches [ 'ProductName']}
+                    ~
Nome de tipo ausente depois de '['.
No linha:12 caractere:144
+ ... ber = $Matches [ 'SystemBoardSparePartNumber']}
+                    ~
Token '[' inesperado na expressão ou instrução.
No linha:12 caractere:145
+ ... er = $Matches [ 'SystemBoardSparePartNumber']}
+                    ~
Nome de tipo ausente depois de '['.
No linha:13 caractere:100
+ ... ber = $Matches [ 'SerialNumber']}
+                    ~
Token '[' inesperado na expressão ou instrução.
Nem todos os erros de análise foram indicados.  Corrija os erros indicados e tente de novo.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken



below the part where has the ip address on file

Management Processor Information:
         Type: iLO4
         Name: ILOBRC5106710
         Firmware Version: 30.02 Aug 19 2015
         IP Address: 192168128224
         MAC Address: C4: 34: 6B: C3: 7B: BF
         Power Management Controller Version: 3.3
         iLO Federation Capable: Yes
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Try this;
$InputFile = 'C:\Temp\EHP{0}.txt'
1..8 | ForEach-Object {
	$FileName = $InputFile -f $_
	Switch -regex -File $FileName {
		'\AServer Blade #(?<Blade>\d+) Information:\Z' {	## First line of a blade
			$Result = '' | Select-Object -Property 'File', 'Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory', 'BoardSparePartNumber', 'ManagementIP'
			$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+System Board Spare Part Number:\s*(?<BoardSparePartNumber>.*?)\s*\Z' {$Result.BoardSparePartNumber = $Matches['BoardSparePartNumber']}
		'\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']}
		'\A\s+IP Address:\s*(?<ManagementIP>\S+)\s*\Z' {$Result.ManagementIP = $Matches['ManagementIP']}
		'\A\s+iLO Federation Capable:\s*(?<ManagementIP>\S+)\s*\Z' {	## Last line of a blade
			$Result
		}
	}
}

Open in new window


To add more results, you can basically just copy the "Server Name" line , and replace the relevant parts (the string to search for and the property name):
'\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
Then you need to add the new property to the list in line 5. This list can be reordered to your likings.

On a side note:
If you post code here, please put it inside "Style Code" tags (see the toolbar above the comment field).
Then while copying and pasting the code, it seems like you used some word processor (Microsoft Word) instead of a text editor; there were lots of spaces added where they didn't belong.
Always use an Editor to handle scripts, for example the Powershell ISE or Notepadd++: https://notepad-plus-plus.org/
Even Notepad is better suited than a word processor.
0
 

Author Closing Comment

by:Alex
Comment Utility
very good very good.

Thank you
0
 

Author Comment

by:Alex
Comment Utility
Good afternoon,

I tried to add one more field(ServerBladeType) in the script as directed, but I'm getting error, I'm still missing?


$InputFile = 'C:\Temp\EHP{0}.txt'
1..11 | ForEach-Object {
	$FileName = $InputFile -f $_
	Switch -regex -File $FileName {
		'\AServer Blade #(?<Blade>\d+) Information:\Z' {	## First line of a blade
			$Result = '' | Select-Object -Property 'File','Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory', 'BoardSparePartNumber', 'ServerBladeType','ManagementIP'
			$Result.File = [IO.Path]::GetFileName($FileName)
			$Result.Blade = $Matches['Blade']
            $Result.ServerBladeType = $Matches['ServerBladeType']		}
        '\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
		'\A\s+System Board Spare Part Number:\s*(?<BoardSparePartNumber>.*?)\s*\Z' {$Result.BoardSparePartNumber = $Matches['BoardSparePartNumber']}
		'\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']}
		'\A\s+IP Address:\s*(?<ManagementIP>\S+)\s*\Z' {$Result.ManagementIP = $Matches['ManagementIP']}
		'\A\s+Server Blade Type:\s*(?<ServerBladeType>\S+)\s*\Z' {$Result.ServerBladeType = $Matches['ServerBladeType']}
        '\A\s+iLO Federation Capable:\s*(?<ManagementIP>\S+)\s*\Z' 
        
{	
			$Result
		}
	}
}

Open in new window

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
That's a bit more complicated, because there are two lines with "Type:" per information block, one in "Blade Information" and one in "Management Processor".
Which one do you need?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Alex
Comment Utility
per information block
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
$InputFile = 'C:\Temp\PS\EHP{0}.txt'
1..11 | ForEach-Object {
	$FileName = $InputFile -f $_
	Switch -regex -File $FileName {
		'\AServer Blade #(?<Blade>\d+) Information:\Z' {	## First line of a blade
			$Result = '' | Select-Object -Property 'File','Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory', 'BoardSparePartNumber', 'ServerBladeType', 'ManagementIP'
			$Result.File = [IO.Path]::GetFileName($FileName)
			$Result.Blade = $Matches['Blade']
			$Block = 'Information'
		}
		'\AManagement Processor Information:\Z' {$Block = 'Management'}
        '\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
		'\A\s+System Board Spare Part Number:\s*(?<BoardSparePartNumber>.*?)\s*\Z' {$Result.BoardSparePartNumber = $Matches['BoardSparePartNumber']}
		'\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']}
		'\A\s+IP Address:\s*(?<ManagementIP>\S+)\s*\Z' {$Result.ManagementIP = $Matches['ManagementIP']}
		'\A\s+Type:\s*(?<ServerBladeType>.*?)\s*\Z' {If ($Block -eq 'Information') {$Result.ServerBladeType = $Matches['ServerBladeType']}}
        '\A\s+iLO Federation Capable:\s*(?<ManagementIP>\S+)\s*\Z' {
			$Result
		}
	}
}

Open in new window

0
 

Author Comment

by:Alex
Comment Utility
The script should not bring the information:

Blade Server # 1 Information:      
  Server Blade Type: In the Server Blade Installed
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Sorry, can't follow you. Neither "Server Blade Type" (which you used in your script version) nor "In the Server Blade Installed" is part of the EHP1.txt sample file you attached.
0
 

Author Comment

by:Alex
Comment Utility
You're right, the file I have made available do not have this information.

But some files have the information

Server Blade # 1 Information:
         Server Blade Type: In the Server Blade Installed

I need the script to show this information as well.

Attached file that has information

Thanks a lot for the help.
EHP10.txt
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
$InputFile = 'D:\Service\PS\EHP{0}.txt'
1..2 | ForEach-Object {
	$FileName = $InputFile -f $_
	Switch -regex -File $FileName {
		'\AServer Blade #(?<Blade>\d+) Information:\Z' {	## First line of a blade
			$Result = '' | Select-Object -Property 'File','Blade', 'ServerName', 'ProductName', 'SerialNumber', 'CPU', 'Memory', 'BoardSparePartNumber', 'ServerBladeType', 'ManagementIP'
			$Result.File = [IO.Path]::GetFileName($FileName)
			$Result.Blade = $Matches['Blade']
		}
		'\AManagement Processor Information:\Z' {$Block = 'Management'}
		'\A\s+Server Name:\s*(?<ServerName>\S+)\s*\Z' {$Result.ServerName = $Matches['ServerName']}
		'\A\s+System Board Spare Part Number:\s*(?<BoardSparePartNumber>.*?)\s*\Z' {$Result.BoardSparePartNumber = $Matches['BoardSparePartNumber']}
		'\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']}
		'\A\s+IP Address:\s*(?<ManagementIP>\S+)\s*\Z' {$Result.ManagementIP = $Matches['ManagementIP']}
		'\A\s+Server Blade Type:\s*(?<ServerBladeType>.*?)\s*\Z' {
			If (($Result.ServerBladeType = $Matches['ServerBladeType']) -eq 'No Server Blade Installed') {
				$Result
			}
		}
		'\A\s+iLO Federation Capable:\s*(?<ManagementIP>\S+)\s*\Z' {
			$Result
		}
	}
}

Open in new window

1
 

Author Comment

by:Alex
Comment Utility
Perfect.

Thank you very much.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
A procedure for exporting installed hotfix details of remote computers using powershell
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

9 Experts available now in Live!

Get 1:1 Help Now