JavaScrip Zone


新发表

新评论

留言信息

链接收藏

『中国群落博客』

免费注册博客通道

快速登陆


结束进程和重新启动的vbs脚本

'function:
' list all process or kill one of them
'parameter:
NameorPID process's name or pid
'return:
' true if kill one process, else false
Function KillProcess(NameorPID)
    Dim oWMI, oProcs, oProc, strSQL
    KillProcess = False
    strSQL = "SELECT * FROM Win32_Process"
    If NameOrPID <> "" Then
        If IsNumeric(NameOrPID) Then
            strSQL = strSQL & " WHERE Handle = '" & NameorPID & "'"
        Else
            strSQL = strSQL & " WHERE Name = '" & NameorPID & "'"
        End If
    End If
    Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
    Set oProcs = oWMI.ExecQuery(strSQL)
    For Each oProc In oProcs
        If IsNumeric(NameOrPID) Then
            oProc.Terminate
            WScript.Echo  oProc.Name & "(" & oProc.Handle & ") was killed!"
            KillProcess = True
        Else
            WScript.Echo "Name: " & oProc.Name & vbTab & "PID: " & oProc.Handle & _
                vbCrLf & vbTab & "Path: " & oProc.ExecutablePath
        End If
    Next
    Set oProc = Nothing
    Set oProcs = Nothing
    Set oWMI = Nothing
End Function

'function:
' reboot or shutdown operating system
'parameter:
' RorS "r"=reboot, "s" or others="shutdown"
'return:
' none
Function Reboot(RorS)
    Dim oWMI, oSys, oOpSys
    Set oWMI = GetObject("winmgmts:{(shutdown)}!\\.\root\cimv2")
    Set oOpSys = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each oSys In oOpSys
        If Instr(LCase(RebootOrShut),"r") > 0 Then
            WScript.Echo  "Reboot..."
            oSys.Reboot
        Else
            WScript.Echo  "Shuting down..."
            oSys.Shutdown
        End If
    Next
    Set oOpSys = Nothing
    Set oSys = Nothing
    Set oWMI = Nothing
End Function

如果想试试看,把上面的代码拷贝到记事本,然后加入调用代码保存成c:\t.vbs,然后在Dos命令窗口下输入cscript c:\t.vbs [参数]。

调用代码可以像:

If WScript.Arguments.Count > 0 Then
    KillProcess WScript.Arguments(0)'或者写Reboot WScript.Arguments(0)
Else
    KillProcess '或者写Reboot
End If


原文地址:http://blog.yesky.com/161/jfzlnyf/1063161.shtml


                                                                 2006-1-19 17:03:48

Posted by jser | 阅读全文() | 回复(0) | 引用通告() | 编辑

..................................................
博客的精神于写自己某一刻的思想或心动!
..................................................
当然要是你愿意就是拿来做记事本也可以!
..................................................
群落博客将提供全程免费服务!免费注册!
..................................................
欢迎您成为群落博客的成员!使用之前请先看系统帮助>>>>系统帮助
..................................................

免费注册群落博客【点击完成注册】
..................................................

发表评论:

    昵称:
    密码: (游客无须输入密码)
    主页:
    标题: