VBScript - 重新启动另一个用户的explorer.exe

时间:2011-08-10 19:17:42

标签: vbscript explorer kill


我正在编写一个小的VBScript,在一周的特定日期禁用Windows XP开始菜单中的关闭选项,然后在第二天重新启用它。
该脚本旨在在有限权限用户登录上运行。由于此用户无权更改Windows注册表,因此必须由管理员帐户运行。
我设置了一个计划任务,在有限的用户登录as explained here, point 5下从管理员帐户运行脚本 这是问题:在对Windows注册表应用更改后,我必须重新启动该用户的explorer.exe才能使更改生效。我的脚本无法执行此操作。它可以杀死explorer.exe但由于某种原因无法重启它。
请注意,如果我直接从管理员帐户运行脚本,更改管理员帐户的注册表设置并重新启动管理员帐户explorer.exe,则脚本可以正常运行。
这是代码的一部分:

Option Explicit
Const RegKey = "HKEY_USERS\LIMITED USER SID HERE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoClose"   
Const BackupDay = 5  'sunday = 1
Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")

If Weekday(Date) = BackupDay Then 

    If WshShell.RegRead(RegKey) = 0 Then

        WshShell.Run "msg * __Message Here__"
        Wscript.Sleep 500

        WshShell.RegWrite RegKey, 1, "REG_DWORD"

        RestartExplorer1
        ' RestartExplorer2 

        WScript.quit

    Else 

[...]

Sub RestartExplorer1()

Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess

strComputer = "."
strProcessToKill = "explorer.exe"

Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer _ 
    & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

For Each objProcess in colProcess
    objProcess.Terminate()

Next
End Sub

程序RestartExplorer1应该杀死所有的explorer.exe进程(包括管理员的进程,它没关系,因为他应该注销,所以不应该有一个,除了我可以通过用户名过滤并只杀死用户的一个,但那不是但是,如果从管理员帐户的计划任务中运行,则绝对没有任何问题 RestartExplorer2没有更好的运气:

Sub RestartExplorer2()

WshShell.Run "cmd /c Taskkill /F /IM explorer.exe"
WScript.Sleep 500
WshShell.Run "cmd /c Start explorer.exe"

End Sub

在这种情况下,explorer.exe确实被杀死但由于某种原因它没有重新启动 我到处搜索都没有结果。
非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Windows中的RunAs功能在其他用户帐户下重新启动Explorer.exe进程。试试这个:

Dim objShell, strUsername, strPassword
strUsername = "username"
strPassword = "password"

Set objShell= WScript.CreateObject("WScript.Shell")
objShell.Run "runas /user:" & strusername & " ""explorer.exe"""

WScript.Sleep 100

objShell.Sendkeys strPassword & "~"
WScript.Quit
相关问题