Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: perl robocopy to temp mapped drive.

by 3dbc (Monk)
on Oct 11, 2018 at 15:48 UTC ( [id://1223872]=note: print w/replies, xml ) Need Help??


in reply to Re: perl robocopy to temp mapped drive.
in thread perl robocopy to temp mapped drive.

I attempted to run net use * and pull the output but am getting errors.

#!/usr/bin/perl -w use strict; use warnings; my $destination = qq(\\\\BLAH\\NESTED\\OBFUSCATED\.WITH SPECIAL_CHARS\ +\SHARE\\NAME\\DOWN\\DEEP); netUse ($destination); sub netUse { my @net_use_output; chomp $_[0]; $_ = $1 if($_[0]=~/(.*)\\$/); print "\nnet use \* \"$_[0]\""; open TASK, "net use \* \"$_[0]\"" or die "cannot map $_[0]"; while (<TASK>) { print; #chomp; #chop; #push (@net_use_output, $_); #return $_; } #print join(", ", @net_use_output); #return @net_use_output; }
above code always produces error:
cannot map \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP at ..\ examples\blah.pl line 12, <TASK> line 1. net use * "\\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP"
Even though copying and pasting the same command on the cmd shell works perfectly?

- 3dbc

Replies are listed 'Best First'.
Re^3: perl robocopy to temp mapped drive.
by poj (Abbot) on Oct 11, 2018 at 17:45 UTC

    Try adding pipe to open

        open TASK, '-|',"net use \* \"$_[0]\"" or die "cannot map $_[0]";

    or alternatively

    #!perl use strict; use warnings; use IPC::System::Simple qw/capture/; my $destination = "\\\\BLAH\\NESTED\\OBFUSCATED\.WITH SPECIAL_CHARS\\S +HARE\\NAME\\DOWN\\DEEP"; print netUse($destination); sub netUse { my $path = shift; my $cmd = "net use * $path"; my ($msg) = grep /Drive/, capture($cmd); return ($msg =~ /Drive (.*) is now connected/); }
    poj
      I needed to add the 2>&1| guess that's the pipe you're talking about. Thanks.
      sub netUse { my @net_use_output; my $output; chomp $_[0]; $_ = $1 if($_[0]=~/(.*)\\$/); print "\nnet use \* \"$_[0]\""; #my $output = `net use \* \"$_[0]\" 2>&1|`; open TASK, "net use \* \"$_[0]\" 2>&1|" or die "cannot map $_[0]" +; while (<TASK>) { print "\n"; print; #chomp; #chop; push (@net_use_output, $_); #return $_; } print join(", ", @net_use_output); print "\n"; print "\n"; print "-----\n"; print @net_use_output[0]; $output = $2 if(@net_use_output[0]=~/^(Drive\s)([A-Z])\:(.*)$/); print "\n"; print "\n"; print "-----\n"; print $output; #return @net_use_output; }
      Returns:
      net use * "\\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHARS\SHARE\NAME\DOWN +\DEEP" Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. The command completed successfully. Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. , , The command completed successfully. , ----- Drive S: is now connected to \\BLAH\NESTED\OBFUSCATED.WITH SPECIAL_CHA +RS\SHARE\NAME\DOWN\DEEP. ----- S
      - 3dbc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1223872]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found