Link to home
Start Free TrialLog in
Avatar of Support_38
Support_38

asked on

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
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

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
$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.
Avatar of Support_38
Support_38

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
very good very good.

Thank you
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

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?
per information block
$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

The script should not bring the information:

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

Perfect.

Thank you very much.