0

how are you doing?

I want to execute VBS code in a VBA environment, in this case I am working in the VB editor in MS Excel 2010. Sadly I have no idea how to convert the VBS code so VBA is able to execute it.

To give an idea about what I am working on: I want to execute a VBS command prompt that needs Admin rights. This prompt will restart a process on a different PC in the network. The VBS code works perfectly fine, when executed via cmd. The reason I want to use VBA is that there are a lot PC's in the network and I want to use a loop to execute the VBS code for every PC.

OK, here is the VBS code:

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)

The question is now, how do I get this code sample in to my VBA environment? I know how to execute the VBS file itself through VBA, but that is not enough, because I need a variable. Also the admin rights are up to the question.

OK, here is an update, I have now this code in my VBA, but I am not able to get things started, I also tried to implement the runas admin:

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
6
  • Are you the administrator or do you have admin Rights? Commented Dec 10, 2013 at 19:12
  • 1
    I want to execute VBS code in a VBA environment, so why the vb.net tag? Commented Dec 10, 2013 at 19:12
  • possible duplicate of Calling a .vbs scipt from Excel VBA Commented Dec 10, 2013 at 19:17
  • If you need to connect to a remote PC using specific credentials then it might be simpler to use SWBemlocator.ConnectServer Eg: stackoverflow.com/questions/716571/vbs-computer-info-script Your VBscript should translate fairly easily to VBA. Commented Dec 10, 2013 at 19:17
  • Thanks guys for the input, I will have a look, and yes I have admin rights in the network at question Commented Dec 11, 2013 at 8:31

2 Answers 2

1

In VBA Editor in excel you need to add a reference to 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
Sign up to request clarification or add additional context in comments.

1 Comment

OK, this seems to be the right way. Now I only need to run this code with admin rights. Any suggestions?
0

This site has example code for ending a process via WMI using vba. Take a look. The example closes notepad.exe, but I'm sure you could have it do the same for symantec

1 Comment

Thank you, will take a look and come back with results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.