extract parts of a file with powershell

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
Support_38Asked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
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
 
Darrell PorterEnterprise Business Process ArchitectCommented:
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
 
Darrell PorterEnterprise Business Process ArchitectCommented:
$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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Support_38Author Commented:
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
 
Support_38Author Commented:
very good very good.

Thank you
0
 
Support_38Author Commented:
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
 
oBdACommented:
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
 
Support_38Author Commented:
per information block
0
 
oBdACommented:
$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
 
Support_38Author Commented:
The script should not bring the information:

Blade Server # 1 Information:      
  Server Blade Type: In the Server Blade Installed
0
 
oBdACommented:
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
 
Support_38Author Commented:
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
 
oBdACommented:
$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
 
Support_38Author Commented:
Perfect.

Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.