0

I am trying to copy the files and folders (Including subfolders) to the destination folder (which is getting from User end) by ROBOCOPY. However, I don't have permission to create the folder on destination. I am having the administrator username and password. How to implement this code to use the username/password in this script or batch file. (Don't consider the "project" getting from user)

use strict;
use warnings;
use Cwd;

while(1)
{
    print "Enter the project \& Destination Folder: ";
    my $input_rec = <STDIN>;
    chomp($input_rec);
    if($input_rec=~m/(exit|q|x)/i) {  exit;  }
    elsif(!$input_rec && $input_rec eq '') { }
    elsif($input_rec!~m/(\s|\t)/s) { }
    else
    {
        my ($dept, $dest) = split /\s/, $input_rec;
        print "DEPT: $dept\t$dest\n";
        system("ROBOCOPY \\\\server1\\Robo\\Source\\ISBN \\\\server2\\Robo\\Source\\ISBN /MIR /SEC /SECFIX");
        exit;
    }
}

Or ELSE I need to copy files and folders without ROBOCOPY using perl script to the destination folders with using administrator credentials which is not user can able to access.

The task is, if user requested admin to create a folders/subfolders with rights/permissions/structure as it is in the source original and its should be created by admin as well requested destination by user end. Each and every time admin can't do this hence we have trying to automate this process. User can't access the server to copy the folders.

It would be appreciated if anyone suggested how to solve this.

ssr1012
  • 2,573
  • 1
  • 18
  • 30
  • As you can see [here](https://technet.microsoft.com/en-us/library/cc733145(v=ws.11).aspx) , for `ROBOCOPY` there is no option for setting the user name and password so you need to grant that acces before calling the `ROBOCOPY`. – John Doe Aug 01 '16 at 09:26
  • @John Doe: Could you please suggest if we can able to write in perl script without ROBOCOPY? – ssr1012 Aug 01 '16 at 09:41
  • Have a look on this [link](https://metacpan.org/pod/File%3a%3aCopy%3a%3aRecursive) – John Doe Aug 01 '16 at 09:50
  • @John Doe: There is no rights into the destination path to copy files and folders. Hence I need to provide the username/password in the script to process the same. – ssr1012 Aug 01 '16 at 10:01
  • as in the example [here](http://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file), you may want to create a batch that will grant the admin rights and inside that batch you can call your perl script to do the job that you need – John Doe Aug 01 '16 at 10:13

2 Answers2

1

You can use something intermediate. You can create a scheduled task on windows with the rights of the user. You should create as "One time" task. You can launch as many as you want.

Task view User

You can invoke from your perl script with this command.

SCHTASKS /RUN /TN "Name of the task"

Its an idea. I hope this helps.

Korsakof
  • 56
  • 6
  • `You can create a scheduled task on windows with the rights of the user.` I am not clear on this Shall we give the admin rights to the user? Its not a big task and its tedious – ssr1012 Aug 03 '16 at 13:12
  • The main problem in my scenario it is that "net use" command it is not active and not allowed on the servers and we need to launch scripts to do operations on the server. Also "net use" it´s quite risky to use because you are writing the password on a text plain file. The other way with the scheduled task you just activate the user on the task that you are launching with the correct rights and it´s more difficult to know the user and password used. There is an option that you can check "Run with the highest privileges" if the user is admin you can run as admin or the role that the user has. – Korsakof Aug 04 '16 at 06:40
  • We are creating the exe and its windows hence we get these above issues? – ssr1012 Aug 04 '16 at 06:50
  • I don´t understand quite well. What do you mean with your last comment?How to solve it? To make an exe file is a good step. ;) – Korsakof Aug 04 '16 at 07:22
1
my $source = "\\\\server1\\Robo\\Source\\ISBN";
print "DEPT: $dept\t$dest\n"; #$dest = "\\\\server2\\Robo\\Destination\\ISBN";
system("net use $source /user:.\\\\\administrator xxxx");
system("net use $dest /user:.\\\\administrator xxxx");
system("ROBOCOPY $source $dest /MIR /SEC /SECFIX");

The above lines supports to copy the files and folders as per the actual privileges to the destinations. Hope its great day!