Link to home
Start Free TrialLog in
Avatar of sovran
sovran

asked on

Elevate permissions for a robocopy action within a VB Script

I'm trying to use a VB script to automatically create a default folder structure for projects.  Users do not have permissions to create folders in the destination, only files, because I want them to adhere to the standard folder structure.  So I've been trying to use this script to elevate the permissions for the robocopy process to copy the source folders to the destination, but for the life of me I cannot figure out the nested quotes to pass the full command.  What happens now when I run it is I see the help for runas flash by.  Can someone help me sort out these quotes?

Set WshShell = WScript.CreateObject("WScript.Shell")

source = "\\<servername>\support$\ProjectSetup\DO_NOT_DELETE_Folder_Creates_New_Folder "
robocopy = "c:\windows\system32\robocopy.exe "
WshShell.Run "cmd /c runas /profile /user:<domain>\<username>" & robocopy & source & path &" /E /COPYALL /SEC"
WScript.Sleep 100
WshShell.Sendkeys "<password>~"

Open in new window

Avatar of answer_dude
answer_dude
Flag of United States of America image

First... it looks like you need a space after the <username> and before the robocopy command...

Second... it looks like you're setting your destination to the system "path" variable?  Is that really what you want?  Typically, the Path variable has a lot of "paths" embedded...

Finally... you might try putting some "backslashes" at the end of your source and path so the values look like "C:\test\" instead of "C:\test" -- both should work fine but I like to be explicit about directories.

Other than that the string looks OK...

If you post the exact string you're trying to achieve I can help you tweak it if it's not working.
Avatar of sovran
sovran

ASKER

Thanks for the reply. Good catch on the space, I've added it to the code below.
The "path" variable is because we're capturing user input for the folder name as you can see below.
I know it's hard for someone else to test this one without having the shares, but here it is.
I still just see the flash of the runas command.

'DEFINE INITIAL VAULES

Dim ERAYear, ERAJobNumber, ERAJobName
ERAYear = "2011"
ERAJobNumber = "001"
ERAJobName = "Job Name"

'REDEFINE VAULES WITH INPUT BOXES

Do
ERAYear = InputBox("Enter 4-digit Year", "Year", ERAYear)
Loop Until Len(ERAYear) = 4
Do
ERAJobNumber = InputBox("Enter 3-digit Job Number", "Job Number", ERAJobNumber)
Loop Until Len(ERAJobNumber) = 3
Do
ERAJobName = InputBox("Enter 12-character Maximum Job Name", "Job Name", ERAJobName)
Loop Until Len(ERAJobName) <= 12

'ASSEMBLE NEW FOLDER PATH

Dim Path

Path = (ERAYear & "-" & ERAJobNumber & "_" & ERAJobName)
Path = Replace(Path, " ", "_")
Path = ("P:\" & Path)

MsgBox Path

Set WshShell = WScript.CreateObject("WScript.Shell")

source = "\\erfile\support$\ProjectSetup\DO_NOT_DELETE_Folder_Creates_New_Folder\ "
robocopy = "c:\windows\system32\robocopy.exe "
WshShell.Run "cmd /c runas /profile /user:ericksenroed\proj_adm " & robocopy & source & path &" /E /COPYALL /SEC"
WScript.Sleep 100
WshShell.Sendkeys "eraproj~"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of answer_dude
answer_dude
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
After some more poking around -- I think your problem is related to trying to embed the password code into your script.  See here for an example about how to use password in a script -- but be careful -- your password will be in the clear unless you encode it...

http://www.tek-tips.com/faqs.cfm?fid=2760
Avatar of sovran

ASKER

So when I try the code you provided I can now see robocopy actually start which is awesome!
The folder is not created at the destination however, so I would like robocopy to write a log file so I can see as it also flashes past so quickly.
That's what I'm working on now.
Avatar of sovran

ASKER

This did the trick for me.  Worked like a charm once I corrected my paths.
Thanks!