none
Unmounting Network Share in PowerShell RRS feed

  • Question

  • Greetings,

    I have a script that performs a robocopy to mirror a local drive and a remote drive.  The remote drive is mounted in the beginning of the script with "net use Z: \\remote_server\Data"

    The script runs flawlessly, however I can't seem to get the syntax correct to unmount the share at the end of the script.  I went down the path of Remove-SmbShare but can't seem to get that working.  Please assist with syntax, or other method.

    Many Thanks,

    Steve Mc. 

    Saturday, July 20, 2019 8:12 PM

Answers

  • After a little more research I discovered I can use net use to dismount the share as well.  Example:

    "net use Z: /delete"

    I have tested this and it works, even as a scheduled job when I'm not logged in.   

    Monday, July 22, 2019 3:32 PM
  • JRV is correct, you cannot access a remote file location without a user that has permissions to access it. I suspect that if you check the Scheduled task you will find that it is running as a user.  That being said if Net Use will work then  UNC should work.

    All of that aside if you still want to use Net Use then check out the syntax by typing Net Use /?.

    I have found in the past that if you want to be sure to be able to unmounts the drive use the /Persistent: No option.  Try this (Untested)

    "net use Z: \\remote_server\Data /Persistent:NO"

    "net use Z: /DELETE"

    Monday, July 22, 2019 3:35 PM

All replies

  • There is no need to map a drive. Just use the UNC path in RoboCopy.

                source :: Source Directory (drive:\path or \\server\share\path).
           destination :: Destination Dir  (drive:\path or \\server\share\path).
                  file :: File(s) to copy  (names/wildcards: default is "*.*").

    When in doubt always read the documentation or help.


    \_(ツ)_/

    Saturday, July 20, 2019 9:25 PM
  • UNC will work when Administrator is logged in but it will not work when this script runs at midnight and no one is logged in.  So that why I'm using "net use."  This issue I'm having is the share stays mounted after robocopy has finished and I would like to unmount the share.  

    Monday, July 22, 2019 12:38 PM
  • UNC will work when Administrator is logged in but it will not work when this script runs at midnight and no one is logged in.  So that why I'm using "net use."  This issue I'm having is the share stays mounted after robocopy has finished and I would like to unmount the share.  

    Who told you that bit of nonsense.  UNC always works.  There is no need to map a drive ever. 

    There is no way to run a process without a logged in session. 

    I think you need to step back and learn the basics of Windows.  UNC can always be used on all modern software.  Mapped drive are legacy and only needed for old applications that do not support UNC paths.


    \_(ツ)_/

    Monday, July 22, 2019 12:43 PM
  • "There is no way to run a process without a logged in session. "

    Yes there is, Windows Task Scheduler.  I use it extensively.

    Monday, July 22, 2019 1:16 PM
  • After a little more research I discovered I can use net use to dismount the share as well.  Example:

    "net use Z: /delete"

    I have tested this and it works, even as a scheduled job when I'm not logged in.   

    Monday, July 22, 2019 3:32 PM
  • JRV is correct, you cannot access a remote file location without a user that has permissions to access it. I suspect that if you check the Scheduled task you will find that it is running as a user.  That being said if Net Use will work then  UNC should work.

    All of that aside if you still want to use Net Use then check out the syntax by typing Net Use /?.

    I have found in the past that if you want to be sure to be able to unmounts the drive use the /Persistent: No option.  Try this (Untested)

    "net use Z: \\remote_server\Data /Persistent:NO"

    "net use Z: /DELETE"

    Monday, July 22, 2019 3:35 PM
  • UNC will work when Administrator is logged in but it will not work when this script runs at midnight and no one is logged in.

    This statement is not correct.

    As already noted, there is no need to map and unmap a drive. This creates unnecessary extra steps. A mapped drive is a reference to a UNC path anyway; it is better to eliminate the mapping complication (mapped drives are per-user) and just use the UNC path directly.


    -- Bill Stewart [Bill_Stewart]

    Monday, July 22, 2019 3:46 PM
    Moderator
  • Untrained people are so ridiculously stubborn and refuse to learn anything new. It is just amazing.

    \_(ツ)_/

    Monday, July 22, 2019 9:17 PM
  • "There is no way to run a process without a logged in session. "

    Yes there is, Windows Task Scheduler.  I use it extensively.

    You need to spend a bit more time learning basic Windows. Again, your statement is completely wrong.

    \_(ツ)_/

    Monday, July 22, 2019 9:20 PM