3

I extract string containing a lot of text and both MAC address and UUID. For example:

![LOG[AA:AA:AA:AA:AA:AA, 0A0A0000-0000-0000-0000-A0A00A000000: found optional advertisement C0420054]LOG]!><time="09:07:57.573-120" date="04-19-2017" component="SMSPXE" context="" type="1" thread="2900" file="database.cpp:533"

I would like to strip the output to only display the MAC Address (e.g AA:AA:AA:AA:AA:AA) and UUID (e.g 0A0A0000-0000-0000-0000-A0A00A000000)

I don´t know how to trim the output.

Here is my script:

$Path = "\\AAAAAAAA\logs$"
$Text = "AA:AA:AA:AA:AA:AA"
$PathArray = @()
$Results = "C:\temp\test.txt"


# This code snippet gets all the files in $Path that end in ".txt".
Get-ChildItem $Path -Filter "*.log" |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}

get-content $PathArray -ReadCount 1000 |
foreach { $_ -match $Text}
Martin Brandl
  • 47,498
  • 11
  • 97
  • 129
  • 1
    Check regex for MAC and UUID and use them as a patern: [MAC address regex](http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address) [UUID regex](http://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex) – autosvet Apr 19 '17 at 08:36

1 Answers1

5

Instead of using the Where-Object cmdlet to filter all files, you can use the -Filter switch of the Get-ChildItem cmdlet. Also you don't have to load the content using the Get-content cmdlet yourself, just pipe the files to the Select-String cmdlet.

To grab MAC, UUID I just googled both regex and combined them:

$Path = "\\AAAAAAAA\logs$"
$Pattern = '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}),\s+(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})'
$Results = "C:\temp\test.txt"

Get-ChildItem $Path -Filter "*.log" -File | 
    Select-String $Pattern | 
    ForEach-Object {
        $_.Matches.Value
    } | 
    Out-File $Results
Martin Brandl
  • 47,498
  • 11
  • 97
  • 129