Board logo

标题: [原创] VBS脚本结束可疑进程及短路径与长路径转换等实用函数 [打印本页]

作者: powerbat    时间: 2012-3-6 08:36     标题: VBS脚本结束可疑进程及短路径与长路径转换等实用函数

如需转载请注明出处:http://www.bathome.net/thread-15778-1-1.html
  1. 'VBS脚本结束可疑进程及短路径与长路径转换、Replace加强等实用函数
  2. 'powerbat @ www.bathome.net 批处理之家
  3. Set fso = CreateObject("Scripting.FileSystemObject")
  4. Set sh = CreateObject("Shell.Application")
  5. Set ws = CreateObject("WScript.Shell")
  6. strComputer = "."
  7. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
  8.     & strComputer & "\root\cimv2")
  9. strTempPath = GetLongPath( fso.GetSpecialFolder(2) )
  10. set colProcs = objWMIService.ExecQuery(_
  11.     "SELECT * FROM Win32_Process Where ProcessID>4")
  12. for each objProc in colProcs
  13.   FilePath = objProc.ExecutablePath
  14.   if not IsNull(FilePath) then
  15.     FilePath = GetLongPath(FilePath)
  16.     if ReplaceEx( replace(FilePath,strTempPath,"",1,1,1), _
  17.         "/^\\[0-9]{3}\\[^\\]+$/i", "") = "" then
  18.       'WScript.Echo FilePath
  19.       objProc.Terminate()
  20.       ws.Environment("process").Item("#") = FilePath
  21.       ws.Run "cmd.exe /d /q /c echo y|cacls ""%#%"" /e /d everyone", 0
  22.     end if
  23.   end if
  24. next
  25. Function GetLongPath(strPath)
  26.   GetLongPath = ""
  27.   strPath = fso.GetAbsolutePathName(strPath) 'in case of "C:\boot.ini\.."
  28.   if fso.FileExists(strPath) then
  29.     GetLongPath = sh.NameSpace( fso.GetParentFolderName(strPath) _
  30.         ).ParseName( fso.GetFileName(strPath) ).Path
  31.   elseif fso.FolderExists(strPath) then
  32.     GetLongPath = sh.NameSpace(strPath).Self.Path
  33.   end if
  34. End Function
  35. Function GetShortPath(strPath)
  36.   GetShortPath = ""
  37.   'strPath = fso.GetAbsolutePathName(strPath) 'unnecessary
  38.   if fso.FileExists(strPath) then
  39.     GetShortPath = fso.GetFile(strPath).ShortPath
  40.   elseif fso.FolderExists(strPath) then
  41.     GetShortPath = fso.GetFolder(strPath).ShortPath
  42.   end if
  43. End Function
  44. function ReplaceEx(sSource, sPattern, sReplace)
  45. rem function ReplaceEx uses regular expression.
  46. rem Arg.2(sPattern) should be like in JavaScript, eg: "/hello/gim"
  47.   dim RegEx, Match, Mode, LastSlash
  48.   LastSlash = InStrRev(sPattern, "/")
  49.   Match = Mid(sPattern, 2, LastSlash-2)
  50.   Mode = Mid(sPattern, LastSlash+1)
  51.   Set RegEx = new RegExp
  52.   RegEx.Pattern = Match
  53.   if InStr(1,Mode,"g",1) then RegEx.Global = True
  54.   if InStr(1,Mode,"i",1) then RegEx.IgnoreCase = True
  55.   if InStr(1,Mode,"m",1) then RegEx.Multiline = True
  56. ReplaceEx = RegEx.Replace(sSource, sReplace)
  57. end function
复制代码
其实,这个“可疑进程”是针对具体案例而言,并非通用。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2