Arthor
Arthor

Reputation: 674

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.

Upvotes: 0

Views: 2179

Answers (2)

RetiredGeek
RetiredGeek

Reputation: 3158

Arthor,

Here's how to do it in a loop:

$MapTable = @{
  "A" = "\\192.168.1.10\NAS1\Automate"
  "B" = "\\192.168.1.11\NAS2\Automate"
  "H" = "\\192.168.1.12\NAS3\Automate"
}

$MapTable.GetEnumerator() | ForEach-Object {

  $NPSDArgs = @{Name       = "$($_.Key)" 
                PSProvider = "FileSystem"
                root       = "$($_.Value)" 
                Persist    = $True
               }
  
  New-PSDrive @NPSDargs -WhatIf

} #End ForEach-Obj...

Remove the -WhatIf when you are sure it does what you want.

WhatIf output:

What if: Performing the operation "New drive" on target "Name: B Provider: Micro soft.PowerShell.Core\FileSystem Root: \192.168.1.11\NAS2\Automate".

What if: Performing the operation "New drive" on target "Name: A Provider: Micro soft.PowerShell.Core\FileSystem Root: \192.168.1.10\NAS1\Automate".

What if: Performing the operation "New drive" on target "Name: H Provider: Micro soft.PowerShell.Core\FileSystem Root: \192.168.1.12\NAS3\Automate".

You'll note that the output is not in the same order as the MapTable. This is a "feature" of Hash tables. If you want the output in the same order use the [ordered] decorator on the definition of the table, e.g. $MapTable = [ordered]@{...}.

HTH

Upvotes: 1

RetiredGeek
RetiredGeek

Reputation: 3158

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

Upvotes: 2

Related Questions