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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.