Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Map drives and printers from Perl

by thparkth (Beadle)
on Jun 01, 2008 at 02:56 UTC ( #689489=sourcecode: print w/replies, xml ) Need Help??
Category: Win32 Stuff
Author/Contact Info Andrew Crawford <acrawford@laetabilis.com>
Description: It's very easy to map a drive or a printer on your Windows workstation from Perl. This makes it very practical to write login scripts in Perl. I have not been able to find a pure-Perl method to check for the success of these operations. They always return success and no error variables are set.
use Win32::OLE;
my $workstation=Win32::OLE->CreateObject("WScript.Network");

# Add a printer
my $printerpath='\\\\server\\printername';
$workstation->AddWindowsPrinterConnection($printerpath);

# Make it the default
$workstation->SetDefaultPrinter($printerpath);

# Map a network drive
my $drivepath='\\\\server\\sharename';
$workstation->MapNetworkDrive("X:",$drivepath);
Replies are listed 'Best First'.
Re: Map drives and printers from Perl
by ikegami (Pope) on Jun 01, 2008 at 03:51 UTC

    my $printerpath="\\server\printername";
    should be
    my $printerpath="\\\\server\\printername";

    $workstation->MapNetworkDrive("X:",\\server\path);
    should be
    $workstation->MapNetworkDrive("X:","\\\\server\\path");

    And it would be good if you showed how to check for success.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2020-09-22 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (130 votes). Check out past polls.

    Notices?