1

I'm trying to map network drive on remote computer using Invoke-Command:

Invoke-Command -ComputerName servername -ScriptBlock { net use L: \\server\folder password /user:login}

but it doesn't work. Simple NET USE doesn't work either. I've also tried Powershell: how to map a network drive with a different username/password

So, how to run "cmd command" NET USE on remote computer? Or how to map network drive with powershell? Thank you

EDIT:

When I use

Invoke-Command -ComputerName servername -ScriptBlock { (New-Object -ComObject WScript.Network).MapNetworkDrive 
("M:", "\\server\folder", $false, "login", "password") }

it will create some "shadow" disk that is visible only from remote computer using:

Invoke-Command -ComputerName servername -ScriptBlock { NET USE }

and can be deleted by using:

Invoke-Command -ComputerName servername -ScriptBlock { NET USE /d /y M: }

but I cannot see, delete or add mapped disc, that are visible when I connect to that remote server:(

Community
  • 1
  • 1
culter
  • 5,487
  • 15
  • 54
  • 66
  • 1
    *How* does it "not work"? Do you get an error? Is WinRM enabled on the remote host? – Ansgar Wiechers Sep 13 '12 at 14:26
  • yes, winRM is enabled, i can execute commands like ipconfig and ps commands, but NET USE gives: "New connections will be remembered. There are no entries in the list." – culter Sep 13 '12 at 14:35
  • Ansgar, when I used your command, that you've deleted already, it maps desired disc, but I can not see it on the remote machine. I can see it only when I run Invoke-command with NET USE from my computer.. – culter Sep 13 '12 at 14:41
  • I had deleted my answer, because it's the exact same solution as the accepted answer to the link you posted, which you said did not work for you. Does the command work when you change `$false` to `$true` (i.e. make the mapped drive persistent)? – Ansgar Wiechers Sep 13 '12 at 15:25
  • No, as I wrote, I've tried advices in that link. I can add disks, remove them, display them with net use, but I cannot see them when I connect to remote computer. Then I see another disks(which were mapped locally) and anything I changed through remoce access isn't applied. – culter Sep 13 '12 at 20:57
  • 1
    Do you log in on both computers with the same account? – Ansgar Wiechers Sep 13 '12 at 21:19
  • Good tip, Ansgar. I have tested it, but when I map disk remotely from my computer with my account, I still can't see this disk when I connect to remote computer with the same account :( I start to think, that it's impossible do be done.. – culter Sep 14 '12 at 07:29
  • After running some tests, I can only say that it does work for me. See my updated answer below. – Ansgar Wiechers Sep 14 '12 at 12:51

3 Answers3

1

Try this:

Invoke-Command -ComputerName servername -ScriptBlock {
  (New-Object -ComObject WScript.Network).MapNetworkDrive(
      "L:", "\\server\share", $false, "login", "password"
    )
}

Edit: When I run the following command

Invoke-Command -ComputerName ServerB -ScriptBlock {
  (New-Object -ComObject WScript.Network).MapNetworkDrive(
      "L:", "\\ServerC\share", $true, "DOMAIN\UserB", "password"
    )
}

as DOMAIN\UserA on ServerA, it connects \\ServerC\share as drive L: on ServerB just fine. When I then log in on ServerB as DOMAIN\UserA, I can see the drive, but receive a warning that not all network drives could be reconnected. Which is only natural since UserA and UserB have different passwords. When I click on the drive, the system asks for the credentials of DOMAIN\UserB, and when I enter the password, I can access the share just fine.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

I had to do it using Scheduled Tasks.

Create a standard bat file with the mapping text in it.

`

net use R: /Delete /y
net use R: "\\SERVERNAME\R" /persistent:yes
net use S: /Delete /y
net use S: "\\SERVERNAME\S" /persistent:yes

`

Create XML with Scheduled Taks job info

`

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-03-12T14:19:18.8527017</Date>
    <Author>DOMAIN\USER</Author>
  </RegistrationInfo>
  <Triggers />
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>I:\Scripts\Schedualed Taks XML\Maps.bat</Command>
    </Exec>
  </Actions>
</Task>

`

use a batch File to create Tasks (schtasks.exe)

`

@echo off
if exist "exists.txt" del "exists.txt"
for /F %%A in (SNL.txt) do (

Echo %%A

schtasks.exe /create /f /tn "Map Drives" /XML "I:\Scripts\Schedualed Taks XML\DriveMaps.xml" /s %%A


)
pause

`

NOTE: SNL.txt is a list of computers you wish to crate the task on.

Then use a batch file to run the task (you could make this one Batch file if you want)

`

@echo off
if exist "exists.txt" del "exists.txt"
for /F %%A in (SNL.txt) do (

Echo %%A

schtasks.exe /run /s %%A /tn "Map Drives" 

)

Pause

`

0

According to This article it's not possible without CredSSP? I'm trying to do the same thing =(

HiTech
  • 913
  • 1
  • 16
  • 34