VBscript to Powershell Script .

I thought it would be cool to convert a vbscript to powershell . I took just a simple vbscript from the Hey Scripting Guys column here . The following is the vbscript:


Set objFS = CreateObject("Scripting.FileSystemObject")
Set objRegExp = new RegExp
objRegExp.Pattern = "((?:(\d{1,2}|[a-fA-F]{1,2}){2})(?::|-*)){6}"
Set objFile = objFS.OpenTextFile("C:\logs\logfile.txt")
strFileContents = objFile.ReadAll
arrLines = Split(strFileContents,vbNewLine)


For Each strLine in arrLines
Set colMatches = objRegExp.Execute(strLine)
For Each strMatch in colMatches
WScript.Echo strMatch
Next
Next

This script extracts MAC addresses from a log file . Lets say you had a delimited text file with this info
192.168.1.5,00-00-00-00-00-00,T

Here is what I came up with in powershell

## Extracting a MAC Address


$re = [regex]"((?:(\d{1,2}|[a-fa-f]{1,2}){2})(?::|-*)){6}"
gc ".\log.txt" | % { $matches = $re.Match($_)};$matches.Value

The toughest part is creating the regex .

Happy Holidays

Chris

Advertisements