powershellmapped-drive

Powershell script to map drives, persistent connection not working


The code below allows me to map the appropriate network drives as and when needed.

The problem I have is when I restart the computer, the mapped drives are lost. So I need to run the script again.

$Net = New-Object -ComObject WScript.Network  
$Rename = New-Object -ComObject Shell.Application

#### # Map the network drives
$Net.MapNetworkDrive("A:", '\\192.168.1.10\NAS1\Automate')
$Net.MapNetworkDrive("G:", '\\10.0.0.1\NAS1\VidePro')  

timeout 4

#### # Rename everything
Write-Host "Naming all mapped driver correctly"
$rename.NameSpace("A:\").Self.Name = 'AutoMate 1Gbit' 
$rename.NameSpace("G:\").Self.Name = 'VidPro 10Gbit'

I have tried the following pieces of code unfortunately it does not work:

$Net.MapNetworkDrive("A:", '\\192.168.1.10\NAS1\Automate' -Persist)
$Net.MapNetworkDrive("A:", '\\192.168.1.10\NAS1\Automate' -Persistant)
$Net.MapNetworkDrive("A:", '\\192.168.1.10\NAS1\Automate', -Persist)
$Net.MapNetworkDrive("A:", '\\192.168.1.10\NAS1\Automate' '-Persist'

I am not sure what I am missing here.

Just as a side note. There are no credentials in the above script as I usually log in and to the credentials first and then run the script. Just an extra security measure on my behalf.

Thank you.


Solution

  • This should do the trick:

    $NPSDArgs = @{Name       = "A" 
                  PSProvider = "FileSystem"
                  root       = "\\192.168.1.10\NAS1\Automate" 
                  Persist    = $True
                 }
    
    New-PSDrive @NPSDargs
    

    To remove the drive:

    Remove-PSDrive -Name "A"
    

    HTH