Home > Back-end >  run a vbscript from usb "AS ADMIN" and get drive letter to ru batch script stored in usb d
run a vbscript from usb "AS ADMIN" and get drive letter to ru batch script stored in usb d

Time:07-09

I have following vbscript:

set objShell = Createobject("wscript.shell")
objShell.Run """Scripts\Rec.bat""", 0
objShell.Run """Scripts\Prod.bat""", 0
Set objShell = Nothing

The above vbscript is stored in USB drive and i am trying to launch it from usb. Now problem is when i normally run this vbscript it executes. But when i do run as administrator from context menu following error occurs "System cannot find the file specified"

I tried to debug the problem using another below script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
Wscript.Echo strDrive

When i run it normally it echo F: that is my usb drive letter. But when i run as admin it echo C:

So the batch file location is being searced to C:\Scripts\Rec.bat instead of F:\Scripts\Rec.bat thats why this error is comming.

Can anyone help??

I want to run vbscript as admin and still retain the usb drive letter path because the code in my Rec.bat requires admin privilege

CodePudding user response:

You don't want it to be relative to the current directory, you want it to be relative to the directory of the running vbscript:

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 
strPath = "" & strFolder & "\Scripts\Rec.bat" & ""

objShell.Run strPath, 0

If you want to try it with a test script, you'd be advised to replace 0 on the last line with 1.

  • Related