Powershell: Map/Remove network-drives on remote computer

Unanswered Powershell: Map/Remove network-drives on remote computer

  • Thursday, January 17, 2013 8:53 AM
     
      Has Code

    Hi,

    I want to check which network drives are mounted to a remote computer and mount missing ones or remove unneeded network drives.

    My goal is to automate this proccess and via script linked to a scheduled task.

    Invoke-Command -ComputerName ComputerName -ScriptBlock { $netobj=New-Object -ComObject WScript.Network
                                                             $netobj.EnumNetworkDrives() #Enum network drive
                                                             $netobj.MapNetworkDrive("Z:","\\server\bla") #Maps netweok drive
                                                             $netobj.RemoveNetworkDrive("Z:") #Removes network drive
                                                           }

    I've tried to run the command above but I does not return any information.

    If i run the command on the local machine it returns the information of the connected resources.

    I cant map stuff via remote because I get an Access Denied error.

    I cant remove stuff via remote because I dont get information about mounted resources.

    Enable-PsRemote (checked)

    WinRM (checked)

    Firewall (checked)

    Permissions on network drives (checked)

    Any idea whats wrong?

    BR


    ..next time we eat bacon






    • Edited by exit1337 Thursday, January 17, 2013 9:54 AM
    •  

All Replies

  • Thursday, January 17, 2013 10:41 AM
     
     
    You cannot map or unmap drives remotely.  Srives are mapped to a user account and a remote scritp will not run in the context of thee logged in user.

    ¯\_(ツ)_/¯

  • Thursday, January 17, 2013 7:58 PM
     
     
    Add this to a log on script and when users log in it will run under their account.
  • Thursday, January 17, 2013 11:14 PM
     
     
    Add this to a log on script and when users log in it will run under their account.

    Add what?

    ¯\_(ツ)_/¯

  • Monday, January 21, 2013 5:45 PM
    Moderator
     
     

    I suspect the problem is that well known 'second hop' issue. You are asking a script you run on a remote computer to create a network share on that system. To do that, the system you are remoting to (when you call Invoke-Command) has to do logon to the server you are pointing to in order to create the network drive.To resolve this, use CredSSP.  That involves three things:

    1. Setup Credssp on both your and the remote systems. See the web for details! :-)

    2. Use the -CredSSP parameter when invoking the script block.

    3. Add the credentials you want to use (ie create a credential object and add that to the call to Invoke-Command.


    Thomas Lee <DoctorDNS@Gmail.Com>

  • Monday, January 21, 2013 5:46 PM
    Moderator
     
     
    JRV is nearly right. By default what he says is correct. However you can use CredSSP to enable the remote server to use the logged on users's credentials to create the network drive.

    Thomas Lee <DoctorDNS@Gmail.Com>

    • Marked As Answer by exit1337 Wednesday, February 27, 2013 10:49 AM
    • Unmarked As Answer by exit1337 Wednesday, February 27, 2013 10:49 AM
    •  
  • Wednesday, February 27, 2013 1:49 PM
     
     
    JRV is nearly right. By default what he says is correct. However you can use CredSSP to enable the remote server to use the logged on users's credentials to create the network drive.

    Thomas Lee <DoctorDNS@Gmail.Com>

    However you really can't.


    ¯\_(ツ)_/¯

  • Wednesday, March 06, 2013 2:53 PM
     
     

    I know it's a scripting forum but is there any reason you wouldn't just use a preference object?  It works really well and allows you to do some really specific targeting. 


    • Edited by R Jason Morgan Monday, April 29, 2013 12:59 AM Clarification
    •