【发布时间】:2015-05-09 03:58:27
【问题描述】:
我花了几天的时间试图找到一种方法来保存用户打开的 Excel 2007 文件,然后再发出 shutdown.exe 或 psshutdown.exe。我正在使用启用了 UAC 的 Windows 7 Pro 64 位机器,包括远程限制。我不想绕过这些安全措施。
我开发了可以完成这项工作的 vbscript,但它只能在本地运行。代码如下:
Option Explicit
'--------------------------------------------------
'*** Dimension Local Variables ***
Dim objXL, msg, i, WshShell, strShutdownMessage, strMsg
'*** Enable Error Handling ***
On Error Resume Next
'*** Display Server Emergency and Computer Shutdown Message ***
Set WshShell = WScript.CreateObject("WScript.Shell") 'Creates an instance of the Windows Scripting Host shell (WshShell)
'--- Popup Message - Automatically Closes After 15 Seconds ---
'--- (the "4112" nType value is the sum of 16 - Critical window type + 4096 - display on top!!)
WshShell.Popup "AN UNEXPECTED SERVER EMERGENCY HAS OCCURRED." & Chr(13) & Chr(13) &_
"THIS COMPUTER WILL AUTOMATICALLY SHUT DOWN 30 SECONDS FROM WHEN THIS MESSAGE DISAPPEARS. " &_
"PLEASE SAVE YOUR DATA AND CLOSE ALL OPEN APPLICATIONS IMMEDIATELY!", 15, "SERVER EMERGENCY SHUTDOWN WARNING!",4112
'*** Debugging Exit ***
'WScript.quit
'*** Wait 30 Seconds Before Saving Data, Closing Applications, and Shutting Down Computer ***
WScript.Sleep 30000
'*** Set Excel Object Variable ***
Set objXL = GetObject( , "Excel.Application")
'*** Trap Error And Shutdown Computer If No Open Excel Workbooks ***
If Err.Number > 0 Then 'No open Excel workbooks
'MsgBox ("ErrorLevel ... = " & Err.Number)
'Wscript.quit
WshShell.run("c:\windows\system32\shutdown.exe /f /s") 'Run shutdown command
End If
'*** Save Data And Close Each Open Excel Workbook ***
For Each i In objXL.Workbooks 'Close each open Excel workbook
i.Save
i.Close
Next
'*** Shutdown Computer ***
WshShell.run("c:\windows\system32\shutdown.exe /f /s")
我遇到的问题是代码执行以下语句时抛出429错误:
Set objXL = GetObject( , "Excel.Application")
我已经尝试过 psexec、runas、vmrum(远程机器是 VM),甚至是任务计划程序来解决这个问题。我什至想出了如何确定远程 VM 上登录用户的会话号,以便我可以将它传递给 psexec,以便它可以与远程 VM 用户交互运行。关闭警告消息正确显示在远程 VM 活动用户的桌面上。但我总是收到 429 错误 - 除非我使用远程 VM 的本地管理员帐户登录本地和远程计算机(这是一个工作组,而不是域,顺便说一句)。我最初认为这可能是一个 psexec 问题,但我现在认为它是 Office 2007 或 Windows 7 UAC 权限问题(感谢 Harry Johnston 对我原帖的回答):
GetObject() 429 使用 PSExec 在虚拟机上运行 WScript 文件时出错 机器
在发出关机指令之前,有什么方法可以在远程机器上保存打开的 Excel 文件吗?我不信任 Office 自动保存。我客户的数据太重要了,不会因为停电或无法正确使用自动保存而丢失。
【问题讨论】:
标签: windows excel vbscript uac psexec