-1

I have 2 vb scripts I'd like to merge in one script.

the first one runs a batch file hidden (invisible window). the second one runs the batch file as a different user. what I need is to have one vb script that runs the batch file as a different user and invisible at the same time.

1)

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Folder\BatchFileName.bat" & Chr(34), 0
Set WinScriptHost = Nothing

2)

Option explicit
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")
oShell.Run "RunAs /noprofile /user:domain\domainuser ""C:\Folder\BatchFileName.bat"""
WScript.Sleep 100
oShell.Sendkeys "yourpassword~"
Wscript.Quit

any help is much appreciated

2
  • 1
    Read about Subroutines ( msdn.microsoft.com/en-us/library/dz1z94ha.aspx ). Since you do not know what a subroutine is, then I would suggest you examine very carefully the scripts you are copying.
    – Yorik
    Jun 14, 2016 at 16:49
  • Hi there, we're not a script writing service. Where are you getting stuck exactly? What has your research shown you so far? What have you tried already? (hint, look up the wscript.shell Run method and learn how it works). Jun 14, 2016 at 19:49

1 Answer 1

0

If you're trying to execute this script as another user without an end-user seeing it than there is no way to combine these scripts.

oShell.Run "RunAs /noprofile /user:domain\domainuser ""C:\Folder\BatchFileName.bat"""
WScript.Sleep 100
oShell.Sendkeys "yourpassword~"

SendKeys relies on the console being an active window, which makes it available to the end-user so that the script will automate sending the keys to the interface faster than the user is capable of interfering. If the user is already typing at the time this executes, your code is flawed and the execution will be interrupted. If you run this script and hold down ANY key, you will prevent it from working properly. In other words, no you can't combine them.

If you're trying to run a script on the background under a different user account, use the scheduling service to execute "%SYSTEMROOT%\system32\cmd.exe /c cscript yourscript.vbs 2>&1 %SYSTEMROOT%\logs\myscript.log" The scheduling service will ask for the credentials if you want it to run as a specific user.

I can't recommend using /savecred under the runas syntax as the end-user will have the ability to runas with that user any time they please. The most secure and effective method to your disposal is using scheduled tasks with your suppressed UI execution.

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run "cmd.exe /c C:\Folder\BatchFileName.bat", 0
Set WinScriptHost = Nothing

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .