Search code examples
powershellvirtual-machinenetwork-drivemicrosoft-file-explorernew-psdrive

How to enable MAP drive on remote computer session


I've been scratching my head for a long. I've two Windows servers joined to Domain and DFS is installed. So when I'm trying to create a MAP drive remotely from server1 to server2 code is not getting an error and it is showing the drive is created but when I log on to server2 to check, it is not showing the mapped drive. I've tried many methods but it is not working out for me when I execute the script manually to the remote server it gets created only while remote execution I'm facing the issue. This is the code I'm using

Invoke-Command -ComputerName $SecondaryServer -ScriptBlock { 
Register-PSSessionConfiguration -Name Demo -RunAsCredential 'abc.local\Administrator' -Force }

Invoke-Command -ComputerName $SecondaryServer -ScriptBlock {

 New-PSDrive -Persist -Name "T" -PSProvider "FileSystem" -Root "\\abc.local\Demo1" -ErrorAction stop
 } -ConfigurationName Demo 

The above code is not generating any error.


Solution

  • Try using -Scope Global.
    And why using separate invoke session ! , single session would work.

    Invoke-Command -ComputerName $SecondaryServer -ScriptBlock { 
    Register-PSSessionConfiguration -Name Demo -RunAsCredential 'abc.local\Administrator' -Force
    New-PSDrive -Persist -Name "T" -PSProvider "FileSystem" -Root "\\abc.local\Demo1" -ErrorAction stop -Scope Global
     } -ConfigurationName Demo 
    

    Check out https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7.2&viewFallbackFrom=powershell-6

    New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public" -Scope Global