Solved

extract parts of a file with powershell

Posted on 2016-08-24
14
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41769610
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
ID: 41769615
$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
ID: 41769620
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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41769775
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
ID: 41769974
very good very good.

Thank you
0
 

Author Comment

by:Alex
ID: 41772085
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 84

Expert Comment

by:oBdA
ID: 41772110
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
 

Author Comment

by:Alex
ID: 41772138
per information block
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41772169
$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
ID: 41772219
The script should not bring the information:

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

Expert Comment

by:oBdA
ID: 41772235
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
ID: 41772245
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 84

Expert Comment

by:oBdA
ID: 41772268
$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
ID: 41772287
Perfect.

Thank you very much.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…

730 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