replace - Trying to remove all spaces, tabs, carriage returns using VBScript -
i trying assign results of command below variable. goal obtain pid number.
service_name: msftpsvc type : 10 win32_own_process state : 3 stop_pending (stoppable, not_pausable, accepts_shutdown) win32_exit_code : 0 (0x0) service_exit_code : 0 (0x0) checkpoint : 0x0 wait_hint : 0x0 pid : 7888 flags :
if have better idea let me know. trying remove spaces, tabs, carriage returns, etc... search "pid:" string , "flags" , copy pid number.
i haven't been able remove spaces , have in 1 single string. here code:
dim wshshell set wshshell = wscript.createobject("wscript.shell") dim commandtorun, scriptstdout, scriptstderr, exitcode dim results commandtorun = "sc.exe queryex msftpsvc" exitcode = execscript(commandtorun) results = replace(scriptstdout, vbcrlf, "") 'this works results = replace(results, vbtab, "") 'nothing happens results = trim(results) 'nothing happens function execscript(command) dim wshshellexec set wshshellexec = wshshell.exec("cmd /c " & command) dim errorread scriptstdout="" scriptstderr="" while wshshellexec.status = 0 while not wshshellexec.stdout.atendofstream scriptstdout=scriptstdout&wshshellexec.stdout.readall() loop while not wshshellexec.stderr.atendofstream errorread = true scriptstderr=scriptstdout&wshshellexec.stderr.readall() loop loop execscript=wshshellexec.exitcode wscript.echo scriptstdout set wshshellexec=nothing end function
thank you
there not tabs in output, , trim
removes spaces beginning , end of string, not somewhere in middle. since in case there no spaces before service_name
or after last colon of multiline string there's nothing trim
remove.
it's better kind of replacement regular expression:
set re = new regexp re.pattern = "\s+" re.global = true results = trim(re.replace(scriptstdout, " "))
the above replace consecutive whitespace (linebreaks, tabs, spaces, etc.) single space, , remove remaining leading or trailing space.
however, since actual goal obtain pid of service, i'd recommend drop approach entirely , switch wmi:
set wmi = getobject("winmgmts://./root/cimv2") qry = "select * win32_service name = 'msftpsvc'" each svc in wmi.execquery(qry) pid = svc.processid next wscript.echo pid
Comments
Post a Comment