批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[原创] 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
复制代码
其实,这个“可疑进程”是针对具体案例而言,并非通用。
1

评分人数

返回列表