0

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.

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Festival think you very much for the information. I have a question. Can this fitting one line. Because I have many drives that I need to map?
For example: New-PSDrive Name = "A", PSProvider = "FileSystem", root = "\\192.168.1.10\NAS1\Automate", Persist = $True The reason I would like to have as a single line is that I have many drives to map.
I also tired: New-PSDrive -Name A FileSystem -Root \\192.168.1.10\NAS1\Automate -PSProvider -Scope Global
Arthor, if you have a lot of drives to map you could place the Name and Root in a hash table or array and just loop through it.
I would love to. But I am not at you level thus far. However, I do have this would not... I only have 7 drive to map. New-PSDrive -Name A FileSystem -Root \\192.168.1.10\NAS1\automate –Persist -Scope Global I think everything is correct.
1

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

1 Comment

Thank you.. I will try this out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.