• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1348
  • Last Modified:

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

0
sovran
Asked:
sovran
  • 3
  • 3
1 Solution
 
answer_dudeCommented:
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.
0
 
sovranAuthor Commented:
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

0
 
answer_dudeCommented:
if you run the resulting string from the command prompt does it work?

Try this... I think the "runas" command requires some quotes around the command you want to "runas"
WshShell.Run "cmd /c runas /profile /user:ericksenroed\proj_adm " & chr(34) & robocopy & source & path &" /E /COPYALL /SEC" & chr(34)

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
answer_dudeCommented:
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
0
 
sovranAuthor Commented:
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.
0
 
sovranAuthor Commented:
This did the trick for me.  Worked like a charm once I corrected my paths.
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now