Extracting Parameters from a File Using Regular Expressions

Extracting Parameters from a File Using Regular Expressions

Extracting parameters from a file using regular expressions can be a very effective and practical way of doing things. Suppose, for instance, that you need a logon script that maps drives depending on the computer’s sites. A custom parameter file could look like this:

Site1.MapDrive_X=\server1share1Site1.MapDrive_Y=\server1share2Site2.MapDrive_X=\server3share1Site3.MapDrive_Z=\server4share4

Extracting those parameters would be as easy as getting the actual site name, building a regular expression, and looping on found matches. Assuming that you’ve got a ParameterFileContent variable already available, you’d have something like this:

set oRegExp = new RegExpoRegExp.Global = True 'To get all matchesoRegExp.IgnoreCase = True 'We don't want to bother with case in our parameter file'Mapping drives for the current ActiveDirectory siteSet oAdSysInfo = CreateObject("ADSystemInfo")oRegExp.Pattern = oAdSysInfo.SiteName & ".MapDrive_(w)=(.*)" Set oMatchCol = oRegExp.Execute(ParameterFileContent)For Each oMatch In oMatchCol '  DriveName = oMatch.SubMatches(0) 'The w from the first set of parenthesis  SharePath = oMatch.SubMatches(1) 'The .* from the second set of parenthesis  WScript.Echo("DriveName=" & DriveName & " SharePath=" & SharePath)  WshNetwork.MapNetworkDrive DriveName & ":",SharePath,FalseNextReuse the oRegExp object to process any other needed parameter...GroupName2.RunApplication=\server1share1Application1Application1.exeGroupName2.MapNetworkDrive_X=\server1share1  Site1.SQLServerName=SQL01
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved