找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 18096|回复: 0

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

[复制链接]
发表于 2012-3-6 08:36:40 | 显示全部楼层 |阅读模式
如需转载请注明出处: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
复制代码
其实,这个“可疑进程”是针对具体案例而言,并非通用。

评分

参与人数 1PB +20 技术 +1 收起 理由
zqz0012005 + 20 + 1 原创鼓励

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:03 , Processed in 0.018912 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表