Sometimes we have list of server names, IPs, name etc. Some people are used to split the characters into arrays and then put it back together to find some specific information from the name
Lets look into regex and how to find Lync servers around the world from a list of servers in a text file. We are going to work from the following list for this exercise.
AZD-DC01 — Arizona, Data Center, Domain Controller
NJD-FS10 — New Jersey, Data Center, File Server
LOD-LYNC01 — London, Data Center, Microsoft Lync
CHD-SHP01 — Chicago, Data Center, Sharepoint
NYD-AZD100 — New York, Data Center, Azure Active Directory
NVD-DC01 — Nevada, Datacenter, Domain Controller
NVO-LYNC10 — Nevada, Office, Microsoft Lync
NYO-WEB01 — New York, Office, web server
NYD-LYNC01 — New York, Data Center, Microsoft Lync
NYD-LYNC02 — New York, Data Center, Microsoft Lync
As you can see the first two character are state. The first thing I usually do with regex is to use the expressions ‘^’. The anchor is used to tell the expression to start at the beginning of the string.
Now we know the states abbreviations starts AL Alabama and ends in WY Wyoming. Based on this we can say that the first character starts in A and finish on W. For the second character we can start in A and finish with Z
the expression will look something like this
The next character is the location and based on the server name nomenclature D is for Data center or O for Office. Regex gives the ability to use the or operator “|”. Our expression will look something like this
You would thing the next expression to use would be ‘[-]’ but this would be incorrect. This expression means any character. What w need to use here is ‘\W’ Capital W means a non word character like ‘*-+=)’. The expression will look like this
Now we need to match on lync, this is is the easiest part using the word boundary expression \b
Lastly we need to match on the digits using ‘d’. ‘d\+’ matches on n number of digits
If we have a document with a bunch of server names and we want to find all of the lync servers we would do the following
As you can see it match all of our server names in the text file. You can use this against active directory using get-adcomputer.