I have a need to connect to the C$ and D$ shares on various windows servers. I want to loop through the servers one at a time, and re-use the same local drive letter for the share. I'm having trouble getting the disconnect subroutine to delete the share before it tries to connect to the next server.

I am using the following command to make the connection (this works OK):

   system("net use ${drive} \\\\${server}\\${remote_drv_ltr}\$ ${passwd} /USER:${domain}\\${login}");

I cobbled together an 'unmount' routine to disconnect the share before going on to the next server. Retrying the delete command every 2 seconds for up to 30 seconds before giving up (this doesn't work reliably).

while ( system("net use $drive /DELETE") ) { : (I can provide the details if necessary) }

I've fumbled around with Win32::NetResource but can't find a good example of how to get it to map a drive, and later release it. Here's what I've got (which doesn't work at all):

use Win32::NetResource qw(:DEFAULT GetSharedResources GetError AddCo +nnection CancelConnection); my $Domain = 'aaaaaaaa'; my $Account = 'bbbbbbbb'; my $Password = 'cccccccc'; my $Server = 'dddddddd'; my $LocalDrive = 'X:'; my $UserName = '${Domain}\\${Account}'; my $Connection = 0; my $ShareInfo = { 'path' => "C:\\temp", 'netname' => "MyShare", 'remark' => "It is good to share", 'passwd' => "", 'current-users'=> 0, 'permissions' => 0, 'maxusers' => -1, 'type' => 0, }; my %NETRESOURCE = ( 'Scope' => 'RESOURCE_REMEMBERED', 'Type' => 'RESOURCETYPE_DISK', 'DisplayType' => 'RESOURCEDISPLAYTYPE_SHARE', 'Usage' => 'RESOURCEUSAGE_CONTAINER', 'LocalName' => "$LocalDrive", 'RemoteName' => "\\\\$Server\\D\$", 'Provider' => "$Server" ); AddConnection(\%NETRESOURCE,$Password,$UserName,$Connection); # CancelConnection($Name,$Connection,$Force);

I'm sure I'm way off (as evidenced by my lack of results). Do any of you have any constructive suggestions, alternate solutions, or (preferably) some sample code to help me get Win32::NetResource's "AddConnection" and "CancelConnection" to do what I need?


In reply to How do you Map & Disconnect Network Drives (in Windows)? by perlofwisdom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.