locked
Disconnect all drives using same path RRS feed

  • Question

  • Hi Guys,

    How should I disconnect all drives that are using same path. Made mistake on GPO mapping and its duplicating to end users.

    Any bat file please.

    Thanks.

    Thursday, November 24, 2016 8:28 AM

Answers

  • How was this done in a GPO?

    There is missing information.  Drives mapped under SYSTEM cannot be managed by a user. If the GPO is deleted and not "removed" then it will continue to be applied.

    You can have two different mappings to the same drive under a normal session and an elevated session.

    You really need to start your troubleshooting with GP in the GP forum.

    Your issue is not really a scripting issue.  It is an issue of how to fix a badly applied GPO.  A GPO can be created that removes all mappings and re-applies the correct ones.  The exact fix would depend on how you did this and how you broke the GPO.


    \_(ツ)_/

    Thursday, November 24, 2016 4:01 PM

All replies

  • Just create an opposite GPO to disconnect the connected drives.

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, November 24, 2016 8:30 AM
    Thursday, November 24, 2016 8:30 AM
  • I dont know what are the drives used for mapping since we have configured a dynamic drive letter. I need a script to determine a certain path and disconnect drives that using this certain path.
    Thursday, November 24, 2016 8:44 AM
  • I'm sure you noticed the first pinned post in this forum: 

    This forum is for scripting questions rather than script requests

    Or you try to find something in the Microsoft Tchnet Script Gallery


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 24, 2016 9:14 AM
  • Yes, I've been searching and came with this.

    $Drive = Get-WmiObject -Class Win32_mappedLogicalDisk -filter "ProviderName='\\\\kamote.internal\\data\\DEG'"
    
    Net Use $Drive.Name /delete /y

    And then I'm getting this error:

    net.exe : The syntax of this command is:
    At line:2 char:4
    + Net <<<<  Use $Drive.Name /delete /y
        + CategoryInfo          : NotSpecified: (The syntax of this command is::String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     


    NET USE
    [devicename | *] [\\computername\sharename[\volume] [password | *]]
            [/USER:[domainname\]username]
            [/USER:[dotted domain name\]username]
            [/USER:[username@dotted domain name]
            [/SMARTCARD]
            [/SAVECRED]
            [[/DELETE] | [/PERSISTENT:{YES | NO}]]

    NET USE {devicename | *} [password | *] /HOME

    NET USE [/PERSISTENT:{YES | NO}]

    Thursday, November 24, 2016 10:55 AM
  • So, if you fire your first command line you fill in the variable $Drive. Did you take a look what's in it?

    $Drive

    What's in $Drive.Name?

    $Drive.Name


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 24, 2016 12:11 PM
  • PS H:\> Get-WmiObject -Class Win32_mappedLogicalDisk -filter "ProviderName='\\\\kamote.internal\\data\\DEG'" | Select Name

    Name
    ----
    E:
    I:

    I'm trying to disconnect the Drive E and I from the computer. Other computers might be using different drive letter too.

    Thursday, November 24, 2016 12:55 PM
  • So  .... there are 2 drives. I wouldn't expect that 'net use' is able to deal with Powershell objects. So you will have to loop through all found drives.

    Something like this:

    $Drives = Get-WmiObject -Class Win32_mappedLogicalDisk -filter "ProviderName='\\\\kamote.internal\\data\\DEG'"
    
    Foreach($Drive in $Drives.Name){
        Net Use $Drive /delete /y
    }


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 24, 2016 1:17 PM
  • I'm getting the same problem.

    net.exe : The syntax of this command is:
    At line:4 char:8
    +     Net <<<<  Use $Drive /delete /y
        + CategoryInfo          : NotSpecified: (The syntax of this command is::String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     


    NET USE
    [devicename | *] [\\computername\sharename[\volume] [password | *]]
            [/USER:[domainname\]username]
            [/USER:[dotted domain name\]username]
            [/USER:[username@dotted domain name]
            [/SMARTCARD]
            [/SAVECRED]
            [[/DELETE] | [/PERSISTENT:{YES | NO}]]

    NET USE {devicename | *} [password | *] /HOME

    NET USE [/PERSISTENT:{YES | NO}]

    Thursday, November 24, 2016 2:03 PM
  • I tried it on my client and it works just as expected. You have to have another issue. Maybe a typo?  Did you copy the code from here to your console or ISE or did you type?


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 24, 2016 2:21 PM
  • For whatever reasons, its is working on other machines but not on the others. The error im getting is the one above. Weird.

    Is there some kind of Remove-WmiObject?

    Thursday, November 24, 2016 2:48 PM
  • Why mix apples and bananas:

    Get-PsDrive -PSProvider FileSystem|?{$_.root -eq '\\server\share'}|Remove-PSDrive -Force


    \_(ツ)_/

    • Proposed as answer by jrv Thursday, November 24, 2016 2:56 PM
    Thursday, November 24, 2016 2:56 PM
  • Get-PSDrive is actually will not work.

    Root Value is the drives letter not the share. Probably because I'm using GPO mapping not via scripting?

    Or old version of powershell, which I believed.

    PS H:\> Get-PSDrive | Select Name,Root

    Name Root 
    ---- ---- 
    C C:\  
    E E:\  
    H H:\  
    I I:\  
    Z Z:\  

    Thursday, November 24, 2016 3:15 PM
  • Get-PSDrive is actually will not work.

    Root Value is the drives letter not the share. Probably because I'm using GPO mapping not via scripting?

    Or old version of powershell, which I believed.

    PS H:\> Get-PSDrive | Select Name,Root

    Name Root 
    ---- ---- 
    C C:\  
    E E:\  
    H H:\  
    I I:\  
    Z Z:\  

    Impossible.  Root value is the share if it is a mapped drive.  You cannot un-map physical drives with any utility.  Those drives are all physical drives.

    I suggest taking some time to learn about Windows fundamentals.  Look up what a physical drive is and how Windows OS hardware is implemented.


    \_(ツ)_/

    Thursday, November 24, 2016 3:24 PM
  • This is what a mapped drive looks like:

    PS C:\scripts> Get-PsDrive -PSProvider FileSystem|select name,root
    
    Name Root
    ---- ----
    C    C:\
    D    D:\
    E    E:\
    Z    \\ws701\c$

    "Z" is mapped.  The others are physical.

    This gets all mapped drive:

    PS C:\scripts> Get-PsDrive |?{$_.root -match  '^\\\\'} |select name,root
    
    Name Root
    ---- ----
    Z    \\ws701\c$

    This gets a particular root:

    PS C:\scripts> Get-PsDrive |?{$_.root -eq  '\\ws701\c$'} |select name,root
    
    Name Root
    ---- ----
    Z    \\ws701\c$


    \_(ツ)_/


    • Edited by jrv Thursday, November 24, 2016 3:30 PM
    Thursday, November 24, 2016 3:27 PM
  • Yes. It wasted my time already trying to figure out (whole day actually). Why on other computers root is equal to the UNC path, but on other computers this is equal to drive letter. I'm 100% sure that they are mapped drives.
    Thursday, November 24, 2016 3:31 PM
  • Here's a screenshot of mapped drives.
    Thursday, November 24, 2016 3:36 PM
  • Are they are drives attached to services like iSCSI.  mapping commands cannot remove this kind of drive.


    \_(ツ)_/

    Thursday, November 24, 2016 3:40 PM
  • Check all info returned to discover how the drive is set up.  It is possible to mess up the registry.  Also PS can overmap  drive.  WMI shows connections and targets.  When the target does not exist or is offline it will still show but may not be visible in PowerShell.


    \_(ツ)_/

    Thursday, November 24, 2016 3:45 PM
  • They are all Online and mapped the same via GPO Mapping. Ive make mistake, it duplicates the mapping 3-4 drives were mapped but using same UNC path. Affected is 10 Thousand PCs :)

    Now I need to remove them and remap again.

    Thursday, November 24, 2016 3:54 PM
  • Here's the error.

    Thursday, November 24, 2016 3:58 PM
  • How was this done in a GPO?

    There is missing information.  Drives mapped under SYSTEM cannot be managed by a user. If the GPO is deleted and not "removed" then it will continue to be applied.

    You can have two different mappings to the same drive under a normal session and an elevated session.

    You really need to start your troubleshooting with GP in the GP forum.

    Your issue is not really a scripting issue.  It is an issue of how to fix a badly applied GPO.  A GPO can be created that removes all mappings and re-applies the correct ones.  The exact fix would depend on how you did this and how you broke the GPO.


    \_(ツ)_/

    Thursday, November 24, 2016 4:01 PM
  • foreach($drive in $drives){
          net use $drive.LocalName
    }


    \_(ツ)_/

    Thursday, November 24, 2016 4:04 PM
  • Yeah. Its all my fault. But I know already the issue, problem is that I dont which drives to be removed at user ends since the GPO Mapping I used is "Dynamic or Use first available letter". There's no way to remove the drives without knowing which drive letters. That's why im working on a script to look all drives the using a certain path and disconnect them from the computers.
    Thursday, November 24, 2016 4:06 PM