powershell - Searching through a text file -


i have script searches lastest modified log file. suppose read text file , pick key phrase display line after it.

so far have this

$logfile = get-childitem 'c:\logs' | sort {$_.lastwritetime} | {$_ -notmatch "x|zr" }| select -last 1 $error = get-content $logfile | select-string -pattern "failed modify" 

an example line reads this

20150721 12:46:26 398fbb92 cv failed modify cn=role-x-users,ou=role groups,ou=groups,dc=gyp,dc=gypuy,dc=net mds_e_bad_membership 1 or more members not exist in directory

they key bit of information im trying here

can help? thanks

try this:

$error = get-content $logfile |      where-object { $_ -like "*failed modify*" } |      select-object -first 1 

this provided looking first match in file. select-string cmdlet returns matchinfo object. depending on requirements there might no reason add level of complexity if you're looking pull first occurrence of error in file.

failing this, recommendation debug , step through it. break on get-content call , see $logfile is. run get-content $logfile , see content looks like. select-string on output. see matchinfo.tostring() looks like. maybe you'll see disconnect.

again, recommendation parse manually through file , work where-object cmdlet @ point.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -