Avatar of mjlaroda
mjlarodaFlag for Bahamas asked on

VBScript Absolute path and Runas

Hi all,
I have a vbscript file  I need users to run from their laptop, the users are part of the domain but their laptops are not, so the users must first log in.

I’m running two scripts, one the get the user credentials and run the script.  The other, well is the script.  Problem:  Each user could save the scripts in different folders on their laptops so I have to use absolute path. That’s where I’m having a problem. Here is the script.
Can someone please help?

'Dim folderName
'folderName = "..\.."

dim fso: set fso = CreateObject("Scripting.FileSystemObject")

' directory in which this script is currently running
CurrentDirectory = fso.GetAbsolutePathName(".")

'to use this to build a new path, you can use the BuildPath() function
NewPath = fso.BuildPath(CurrentDirectory, "SetPrintChkFst.vbs") ‘Name of file to run-this part ‘works. **********************
If(fso.FileExists(NewPath))  Then
      WScript.Echo("The file " & NewPath & " exist")
      WScript.Echo("The file " & NewPath & " cannot be found")

End If

set objShell = WScript.CreateObject("WScript.Shell")

strUserName = inputBox("What is your username (domain\username)")

'objShell.Run "runas /user:" & strUserName & " ""wscript.exe" & "(fso.File(NewPath))"" "
objShell.Run "runas /user:" & strUserName & " ""wscript.exe " & "..\..\SetPrintChkFst.vbs"" "
‘Tried a lot here, nothing works ***************************
WScript.sleep 10000

'objShell.Run "runas /user:" & strUserName & " " NewPath
ProgrammingVB Script

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

ok from the echo command I get:
runas /user:rossdm\mlaroda "wscript.exe C:\users\mlaroda\Desktop\SetPrinter Canon\new\SetPrintChkFst.vbs

which is good, but on objectShell.Run command I'm getting:
There is no file extension in "C:\Users\mlaroda\Desktop\SetPrinter".

I've temporary changed that folder to SetPrinter_Canon, now it's showing the full name but still not running the file. meanwhile I'm checking the file itself.

But I will also most likely will need it to work with the space in the folder name

You need to make sure the whole path is in quotes like:
runas /user:rossdm\mlaroda wscript.exe "C:\users\mlaroda\Desktop\SetPrinter Canon\new\SetPrintChkFst.vbs"

Next see what happens if you just paste that line into a command prompt.

I also don't think you need wscript.exe in there. You should be able to just run the vbs. I use Linux at work now, so I can't test that theory, but if it helps...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

I extended the folders for testing purposes to
 C:\Users\mlaroda\Desktop\SetPrinter_Canon\new\Space folder\SetPrintChkFst.vbs

I'm Back to There is no file extension in

using line:

objShell.Run "runas /user:" & strUserName & " ""wscript.exe " & "C:\Users\mlaroda\Desktop\SetPrinter_Canon\new\Space folder\SetPrintChkFst.vbs"

This may be a windows 8 problem to I'll be satisfied if I get it to work without space in the folder for now.

Hi, try this code.  I think the main problem is that when you need to use multiple quotes in the runas string, you need to escape the "inner" ones using a backslash.



Set objFSO = CreateObject("Scripting.FileSystemObject")

' directory in which this script is currently running
CurrentDirectory = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
NewPath = CurrentDirectory & "SetPrintChkFst.vbs"

If objFSO.FileExists(NewPath) = True Then
	WScript.Echo("The file " & NewPath & " exist")
	Set objShell = CreateObject("WScript.Shell")
	strUserName = inputBox("What is your username (domain\username)")
	objShell.Run "runas /user:" & strUserName & " ""wscript.exe \""" & newpath & "\""""", 1, True
	WScript.Echo("The file " & NewPath & " cannot be found")
End If

Open in new window