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
Who is Participating?
TommySzalapskiConnect With a Mentor Commented:
Does it say it finds the file when the if runs?
This line looks like what I would use except it appears to be missing a space and has quotes in the wrong places
bjShell.Run "runas /user:" & strUserName & " ""wscript.exe" & "(fso.File(NewPath))"" "

Try building your run string first and then echoing it so you can see if it looks right.

command = "runas /user:" & strUserName & " ""wscript.exe " & NewPath & """"
echo command
bjShell.Run command
mjlarodaAuthor Commented:
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.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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...
mjlarodaAuthor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.