Hi,
I am trying to create a regular expression to match part of a file name into a variable but I can't seem to get it to work correctly.
Here is a sample from gci:
SERVERNAMEAPPUPP01_Baseline20140220.blg
SERVERNAMEWWSCOOE3_Baseline20140220.blg
SERVERNAMEWWSWEB5_Baseline20140220.blg
SERVERNAMEPNWEBWW01_Baseline20140220.blg
SERVERNAMEPNWEBWW02_Baseline20140220.blg
SERVERNAMEPNWEBWW03_Baseline20140220.blg
SERVERNAMEPNWEBWW04_Baseline20140220.blg
SERVERNAMEPNWEBWW05_Baseline20140220.blg
SERVERNAMEPNWEBWW06_Baseline20140220.blg
SERVERNAMEPNWEBWW07_Baseline20140220.blg
SERVERNAMEPNWEBWW08_Baseline20140220.blg
SERVERNAMEPNWEBWW11_Baseline20140220.blg
SERVERNAMEPNWEBWW12_Baseline20140220.blg
SERVERNAMEPNWEBWW17_Baseline.blg
SERVERNAMEPNWEBWW17_Baseline20140220.blg
SERVERNAMEPNWEBWW18_Baseline20140220.blg
SERVERNAMEPNWEBWW22_Baseline20140220.blg
SERVERNAMEPNWEBWW32_Baseline20140220.blg
SERVERNAMEPNWEBWWI01_Baseline20140220.blg
SERVERNAMEPNWEBWWI11_Baseline20140220.blg
LDVWEBWAABEC01_Baseline20140220.blg
LDVWEBWABFEC02_Baseline20140220.blg
What I am attempting to do is match from the start of the file name to the underscore(not including the underscore). This part of the file name contains the server name.
$files = gci E:\CollectedPerfLogs\20140220 -file -name $pattern ='/[^_]*/' foreach ($file in $files){ $file -match $regex $servername = $Matches[0] Write-Host "The server name is $servername" }
The problem is the regexisn't matching even though I verified it in an online regex tester. I would appreciate any assistance in figuring out why this isn't working.