Link to home
Start Free TrialLog in
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")
Else
      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
WScript.Echo("Ent")
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjlaroda

ASKER

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...
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
"C:\Users\mlaroda\Desktop\SetPrinter_Canon\ner\Space".

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.

Regards,

Rob.

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
Else
	WScript.Echo("The file " & NewPath & " cannot be found")
End If

Open in new window