Extract MAC address and UUID from string

I am extracting a string that contains a lot of text and both the 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 turn off the output to only display the MAC address (for example AA:AA:AA:AA:AA:AA

) and UUID (for example 0A0A0000-0000-0000-0000-A0A00A000000

)

I don't know how to trim the output.

Here's 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}

      

+3


source to share


1 answer


Instead of using a cmdlet Where-Object

to filter all files, you can use a -Filter

cmdlet switch Get-ChildItem

. Also, you don't need to download the content using the cmdlet Get-content

yourself, just pipe the files to the cmdlet Select-String

.

To grab MAC, UUID, I just looked up both regexes 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

      

+5


source







All Articles