Jump to content

Combine 2 vbs scripts


beto

Recommended Posts

Hello to everyone
I'm trying to make a vbs script to to run a batch/cmd file elevated/with admin priviledges. Both files are in the same folder.
I also want to be able to run the script from no matter which drive or directery the folder is placed.
I mean using the equivalent of %~dp0 variable in the vbs script. Right now I have 2 vbs scripts.
One runs the cmd elevated, and the second one can run the cmd from inside the folder where they both are placed.
My problem: I'm unable to put the code of the 2 vbs scripts to one and a single vbs script file. I need your help please.

 

First script to run cmd with admin priviledges:

Set WshShell = WScript.CreateObject("WScript.Shell")
  If WScript.Arguments.length = 0 Then
  Set ObjShell = CreateObject("Shell.Application")
  ObjShell.ShellExecute "wscript.exe", """" & _
  WScript.ScriptFullName & """" &_
  " RunAsAdministrator", , "runas", 1
  Wscript.Quit
  End if

Set WinScriptHost = CreateObject("WScript.Shell")
CreateObject("Wscript.Shell").Run "cmd /k " & chr(34) & "C:\Users\xxxxx\Documents\file.cmd" & chr(34), 0, True
Set WinScriptHost = Nothing

 

Second script to run cmd from the same folder as the vbs script:

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
CreateObject("Wscript.Shell").Run "file.cmd",0,True

Thank you

Link to comment
Share on other sites


  • Replies 3
  • Views 825
  • Created
  • Last Reply

There are other alternatives to a VBScript.  Take a look at these.

 

http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html

http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator#12264592

http://windowsitpro.com/windows-server-2008/automatic-elevation-cmd-script

http://www.addictivetips.com/windows-tips/how-to-open-elevated-command-prompt-with-administrator-privileges-in-windows-vista/

 

Link to comment
Share on other sites


34 minutes ago, straycat19 said:

There are other alternatives to a VBScript.  Take a look at these.

 


http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html

http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator#12264592

http://windowsitpro.com/windows-server-2008/automatic-elevation-cmd-script

http://www.addictivetips.com/windows-tips/how-to-open-elevated-command-prompt-with-administrator-privileges-in-windows-vista/

 

Thank you for the reply.

In fact I'm trying to:

1) Run batch file from vbscript

2) Run elevated

3) Run from the same folder. The folder can be placed anywhere C,D,Usb stick etc

4) Run everything Hidden

The above 2 scripts kind of do what I need, but separately. Don't have the ability to create one single script to do all I'm looking for. I don't just need "to run some script elevated". If someone could put/fix those codes together would be much appreciated.

Link to comment
Share on other sites


For those who might be interested, I got the solution from another forum. Credits go to Dragokas

to:

1) Run batch file from vbscript

2) Run it elevated

3) Run from the same folder. Batch and vbs script in the same folder. The folder can be placed anywhere: C,D,Usb stick etc

4) Run everything Hidden

Here is the code:

Option Explicit

Dim oFSO:       Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oShApp:     Set oShApp = CreateObject("Shell.Application")

Dim sBatName:   sBatName = "File.cmd"

Dim sScriptDir: sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)

sBatName = oFSO.BuildPath(sScriptDir, sBatName)

oShApp.ShellExecute "cmd.exe", "/c" & " " & """" & sBatName & """", sScriptDir, "runas", 0

set oShApp = Nothing: set oFSO = Nothing

Replace "File.cmd" with your file name.

Link to comment
Share on other sites


Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...