【发布时间】:2011-08-10 19:17:41
【问题描述】:
我正在编写一个小的 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
Procedure 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 确实被杀死了,但由于某种原因它没有重新启动。
我到处搜索,没有结果。
非常感谢任何帮助,谢谢。
【问题讨论】: