regex in select-string

This topic contains 3 replies, has 3 voices, and was last updated by  Burps 3 months, 2 weeks ago.

  • Author
    Posts
  • #78084

    Burps
    Participant

    Hello,

    I would like to retrieve some needle in a haystack :

    The haystack is the following :

    @{CommandLine=D:\ibm\WebSphere\AppServer/java/bin/java -Declipse.security -Dosgi.install.area=D:\ibm\WebSphere\AppServer -Dosgi.configuration.area=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration -Dosgi.framework.extensions=com.ibm.cds,com.ibm.ws.eclipse.adaptors -Xshareclasses:name=webspherev80,nonFatal -Dsun.reflect.inflationThreshold=250 -Dcom.ibm.xtq.processor.overrideSecureProcessing=true -Xbootclasspath/p:D:\ibm\WebSphere\AppServer/java/jre/lib/ibmorb.jar -classpath D:\IBM\WebSphere\AppServer\profiles\App Srv01/properties;D:\ibm\WebSphere\AppServer/properties;D:\ibm\WebSphere\AppServer/lib/startup.jar;D:\ibm\WebSphere\AppS erver/lib/bootstrap.jar;D:\ibm\WebSphere\AppServer/lib/jsf-nls.jar;D:\ibm\WebSphere\AppServer/lib/lmproxy.jar;D:\ibm\We bSphere\AppServer/lib/urlprotocols.jar;D:\ibm\WebSphere\AppServer/deploytool/itp/batchboot.jar;D:\ibm\WebSphere\AppServ er/deploytool/itp/batch2.jar;D:\ibm\WebSphere\AppServer/java/lib/tools.jar -Dibm.websphere.internalClassAccessMode=allow -Xms384m -Xmx384m -Xscmaxaot4M -Xscmx60M -Dws.ext.dirs=D:\ibm\WebSphere\A ppServer/java/lib;D:\IBM\WebSphere\AppServer\profiles\AppSrv01/classes;D:\ibm\WebSphere\AppServer/classes;D:\ibm\WebSph ere\AppServer/lib;D:\ibm\WebSphere\AppServer/installedChannels;D:\ibm\WebSphere\AppServer/lib/ext;D:\ibm\WebSphere\AppS erver/web/help;D:\ibm\WebSphere\AppServer/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime -Dderby.system.home=D:\ibm\WebSphere\AppServer/derby -Dcom.ibm.itp.location=D:\ibm\WebSphere\AppServer/bin -Djava.util.logging.configureByServer=true -Duser.install.root=D:\IBM\WebSphere\AppServer\profiles\AppSrv01 -Djava.ext.dirs=D:\ibm\WebSphere\AppServer/tivoli/tam;D:\ibm\WebSphere\AppServer/java/jre/lib/ext -Djavax.management.builder.initial=com.ibm.ws.management.PlatformMBeanServerBuilder -Dpython.cachedir=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/temp/cachedir -Dwas.install.root=D:\ibm\WebSphere\AppServer -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager -Dserver.root=D:\IBM\WebSphere\AppServer\profiles\AppSrv01 -Dcom.ibm.security.jgss.debug=off -Dcom.ibm.security.krb5.Krb5Debug=off -Dfile.encoding=ISO-8859-15 -Dworkspace.user.root=D:/gmf/workspace/wstemp -Xgcpolicy:gencon -Xlp -Xcompressedrefs -verbose:gc -Xverbosegclog:D:/gmf/logs/websphere/appserver/prod2/verbosegc.%Y%m%d.%H%M%S.%pid.log,10,10000 -javaagent:d:/Introscope/IntroscopeAgent9.1.5/wily/Agent.jar -Dcom.wily.introscope.agentProfile=d:/Introscope/IntroscopeAgent9.1.5/wily/core/config/IntroscopeAgent.profile "-Djava. library.path=D:\ibm\WebSphere\AppServer/lib/native/win/x86_64/;D:\ibm\WebSphere\AppServer\java\jre\bin\compressedrefs;D :\ibm\WebSphere\AppServer\java\jre\bin;C:\Windows\system32;C:\Windows;D:\ibm\WebSphere\AppServer\lib\native\win\x86_64; D:\ibm\WebSphere\AppServer\bin;D:\ibm\WebSphere\AppServer\java\bin;D:\ibm\WebSphere\AppServer\java\jre\bin;C:\Windows\s ystem32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\CA\SC\Csam\SockAdapter\bin;c:\Program Files (x86)\CA\DSM\Bin\;D:\IBM\WebSphere\AppServer\bin;D:\IBM\WebSphere\App Server\java\bin;D:\gmf\lib\ext\native\cmododwek;.;" -Djava.endorsed.dirs=D:\ibm\WebSphere\AppServer/endorsed_apis;D:\ib m\WebSphere\AppServer/java/jre/lib/endorsed;D:\ibm\WebSphere\AppServer\endorsed_apis;D:\ibm\WebSphere\AppServer\java\jr e\lib\endorsed -Djava.security.auth.login.config=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/properties/wsjaas.conf -Djava.security.policy=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/properties/server.policy com.ibm.wsspi.bootstrap.WSPreLauncher -nosplash -application com.ibm.ws.bootstrap.WSLauncher com.ibm.ws.runtime.WsServer D:\ibm\WebSphere\AppServer\profiles\AppSrv01\config NTSARN29Node01Cell ntsarn29Node01 prod2}

    And I would like to output the word "prod2", in the following string : "D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration"

    Here is the code until now :

    $pattern ="D:\\ibm\\WebSphere\\AppServer\\profiles\\AppSrv01/servers/(\w*)/configuration"
    Get-WmiObject Win32_Process -computername ${SERVERS} -Filter "name = 'java.exe'" |Select-Object CommandLine |
    	Select-string -pattern $pattern -AllMatches | foreach{$_.Matches}
    

    I have the followinf output :

    Groups   : {D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration, prod2}
    Success  : True
    Captures : {D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration}
    Index    : 147
    Length   : 72
    Value    : D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration
    

    How can I only retrive the "prod2" word ?

    Thanls for your help

  • #78097

    Olaf Soyk
    Participant

    I'm not completely sure if that helps but you should try it with regex lookahead and lookbehind like this:

    $string = '@{CommandLine=D:\ibm\WebSphere\AppServer/java/bin/java -Declipse.security -Dosgi.install.area=D:\ibm\WebSphere\AppServer -Dosgi.configuration.area=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/servers/prod2/configuration -Dosgi.framework.extensions=com.ibm.cds,com.ibm.ws.eclipse.adaptors -Xshareclasses:name=webspherev80,nonFatal -Dsun.reflect.inflationThreshold=250 -Dcom.ibm.xtq.processor.overrideSecureProcessing=true -Xbootclasspath/p:D:\ibm\WebSphere\AppServer/java/jre/lib/ibmorb.jar -classpath D:\IBM\WebSphere\AppServer\profiles\App Srv01/properties;D:\ibm\WebSphere\AppServer/properties;D:\ibm\WebSphere\AppServer/lib/startup.jar;D:\ibm\WebSphere\AppS erver/lib/bootstrap.jar;D:\ibm\WebSphere\AppServer/lib/jsf-nls.jar;D:\ibm\WebSphere\AppServer/lib/lmproxy.jar;D:\ibm\We bSphere\AppServer/lib/urlprotocols.jar;D:\ibm\WebSphere\AppServer/deploytool/itp/batchboot.jar;D:\ibm\WebSphere\AppServ er/deploytool/itp/batch2.jar;D:\ibm\WebSphere\AppServer/java/lib/tools.jar -Dibm.websphere.internalClassAccessMode=allow -Xms384m -Xmx384m -Xscmaxaot4M -Xscmx60M -Dws.ext.dirs=D:\ibm\WebSphere\A ppServer/java/lib;D:\IBM\WebSphere\AppServer\profiles\AppSrv01/classes;D:\ibm\WebSphere\AppServer/classes;D:\ibm\WebSph ere\AppServer/lib;D:\ibm\WebSphere\AppServer/installedChannels;D:\ibm\WebSphere\AppServer/lib/ext;D:\ibm\WebSphere\AppS erver/web/help;D:\ibm\WebSphere\AppServer/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime -Dderby.system.home=D:\ibm\WebSphere\AppServer/derby -Dcom.ibm.itp.location=D:\ibm\WebSphere\AppServer/bin -Djava.util.logging.configureByServer=true -Duser.install.root=D:\IBM\WebSphere\AppServer\profiles\AppSrv01 -Djava.ext.dirs=D:\ibm\WebSphere\AppServer/tivoli/tam;D:\ibm\WebSphere\AppServer/java/jre/lib/ext -Djavax.management.builder.initial=com.ibm.ws.management.PlatformMBeanServerBuilder -Dpython.cachedir=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/temp/cachedir -Dwas.install.root=D:\ibm\WebSphere\AppServer -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager -Dserver.root=D:\IBM\WebSphere\AppServer\profiles\AppSrv01 -Dcom.ibm.security.jgss.debug=off -Dcom.ibm.security.krb5.Krb5Debug=off -Dfile.encoding=ISO-8859-15 -Dworkspace.user.root=D:/gmf/workspace/wstemp -Xgcpolicy:gencon -Xlp -Xcompressedrefs -verbose:gc -Xverbosegclog:D:/gmf/logs/websphere/appserver/prod2/verbosegc.%Y%m%d.%H%M%S.%pid.log,10,10000 -javaagent:d:/Introscope/IntroscopeAgent9.1.5/wily/Agent.jar -Dcom.wily.introscope.agentProfile=d:/Introscope/IntroscopeAgent9.1.5/wily/core/config/IntroscopeAgent.profile "-Djava. library.path=D:\ibm\WebSphere\AppServer/lib/native/win/x86_64/;D:\ibm\WebSphere\AppServer\java\jre\bin\compressedrefs;D :\ibm\WebSphere\AppServer\java\jre\bin;C:\Windows\system32;C:\Windows;D:\ibm\WebSphere\AppServer\lib\native\win\x86_64; D:\ibm\WebSphere\AppServer\bin;D:\ibm\WebSphere\AppServer\java\bin;D:\ibm\WebSphere\AppServer\java\jre\bin;C:\Windows\s ystem32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\CA\SC\Csam\SockAdapter\bin;c:\Program Files (x86)\CA\DSM\Bin\;D:\IBM\WebSphere\AppServer\bin;D:\IBM\WebSphere\App Server\java\bin;D:\gmf\lib\ext\native\cmododwek;.;" -Djava.endorsed.dirs=D:\ibm\WebSphere\AppServer/endorsed_apis;D:\ib m\WebSphere\AppServer/java/jre/lib/endorsed;D:\ibm\WebSphere\AppServer\endorsed_apis;D:\ibm\WebSphere\AppServer\java\jr e\lib\endorsed -Djava.security.auth.login.config=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/properties/wsjaas.conf -Djava.security.policy=D:\IBM\WebSphere\AppServer\profiles\AppSrv01/properties/server.policy com.ibm.wsspi.bootstrap.WSPreLauncher -nosplash -application com.ibm.ws.bootstrap.WSLauncher com.ibm.ws.runtime.WsServer D:\ibm\WebSphere\AppServer\profiles\AppSrv01\config NTSARN29Node01Cell ntsarn29Node01 prod2}'
    $pattern ='(?< =D:\\ibm\\WebSphere\\AppServer\\profiles\\AppSrv01/servers/)\w*(?=/configuration)'
    $string -match $pattern
    $Matches
  • #78103

    Curtis Smith
    Participant

    Actually you can use what you have already. You just need to get the value from the group.

    $pattern ="D:\\ibm\\WebSphere\\AppServer\\profiles\\AppSrv01/servers/(\w*)/configuration"
    Get-WmiObject Win32_Process -computername ${SERVERS} -Filter "name = 'java.exe'" |Select-Object CommandLine |
    	Select-string -pattern $pattern -AllMatches | foreach{$_.Matches.groups[1].value}
  • #78147

    Burps
    Participant

    Great, thank you !

You must be logged in to reply to this topic.