我想在VBA环境中执行VBS代码,在这种情况下,我在MS Excel 2010中的VB编辑器中工作。遗憾的是我不知道如何转换VBS代码以便VBA能够执行它。
要了解我正在处理的内容:我想执行需要管理员权限的VBS命令提示符。此提示将在网络中的其他PC上重新启动进程。当通过cmd执行时,VBS代码完全正常。我想使用VBA的原因是网络中有很多PC,我想使用循环来为每台PC执行VBS代码。
好的,这是VBS代码:
strComputer = "Computername"
Set objShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
intReturn = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -stop", Null, Null, intProcessID)
Wscript.Sleep 10000
intReturn2 = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -start", Null, Null, intProcessID)
现在的问题是,如何将此代码示例导入我的VBA环境?我知道如何通过VBA执行VBS文件,但这还不够,因为我需要一个变量。管理员权利也取决于问题。
好的,这是一个更新,我现在在我的VBA中有这个代码,但我无法启动,我也尝试实现runas管理员:
Sub run_vbs_script()
Dim strComputer As String
'Dim WshShell
Dim objShell
Dim objWMIService
Dim dDate As Date
strComputer = "IEDBR8D60CR"
Set objShell = CreateObject("Wscript.Shell")
RunasStruser = "runas /user:Username Domain\domain name"
objShell.Run RunasStruser, 0
objShell.SendKeys "password"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
intReturn = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -stop", Null, Null, intProcessID)
dDate = DateAdd("s", 10, DateTime.Now)
Do While dDate > DateTime.Now
DoEvents
Loop
intReturn2 = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -start", Null, Null, intProcessID)
Set objShell = Nothing
Set objWMIService = Nothing
End Sub
答案 0 :(得分:1)
在excel的VBA编辑器中,您需要添加对WMI Scripting的引用
Dim strComputer As String
Dim objShell
Dim objWMIService
Dim dDate As Date
strComputer = "Computer"
Set objShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
intReturn = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -stop", Null, Null, intProcessID)
dDate = DateAdd("s", 10, DateTime.Now)
Do While dDate > DateTime.Now
DoEvents
Loop
intReturn2 = objWMIService.Create("c:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.1000.157.105\Bin64\smc.exe -start", Null, Null, intProcessID)
Set objShell = Nothing
Set objWMIService = Nothing
答案 1 :(得分:0)
This site has example code使用vba通过WMI结束进程。看一看。该示例关闭了notepad.exe,但我确信您可以为symantec执行相同的操作