mapping multiple network drives..

by Symbiot at 2012-12-24 01:17:26


I’ve got a script to map 1 network drive… But I am greedy… I want more…

My script does :

if(!(Test-Path -Path $path))
net use $path $share /User:$user $password
“I’ve mounted the drive because it wasn’t there” ##test
“I’m already there dude, but I guess you’ll delete me anyway” ##test
net use * /d /y
net use $path $share /User:$user $password

so now I want to create



and make it do all the nice stuff it does…

But how can I do it?
by Klaas at 2012-12-24 03:03:49
It depends how many shares you want, where do the paths come from (hardcoded in the script, a text document, discovered,…), what “nice stuff” do you want to do?
You can create an array like $paths = @(‘path1’,‘path2’,‘path3’) and loop through that executing the code above with ‘foreach’, or create a function,…

Do you want to delete all shares if the one share you’re looking for existst?

Maybe you can review the logic: since the shares must always be created, that could happen outside the ‘IF’ logic:
if(Test-Path -Path $path) {net use * /d /y}
net use $path $share /User:$user $password
by Symbiot at 2012-12-25 01:19:00
I want to add 5 or 6 shares. The paths are hardcodede into the script. The nice stuff is just mount the drive… nothing else…
I dont necessarily want to delete the drives @ each logon… just verify that they’re there and responding and if not… delete & remount.
I am looking to insert some logic into the script instead of copy pasting the if/else for each drive… that seems silly…
by Symbiot at 2012-12-25 02:21:54
ok, this is what I have:



$paths = @(’$path1’,’$path2’,’$path3’)

Foreach ($path in $paths)
#check if something is mounted if yes, then remove it.
#if nothing is mounted, mount share1, 2 3 etc…

by Symbiot at 2012-12-25 02:24:37
so I should make a $shares = @(’$share1’, ‘$share2’) etc.
then I have shares in an array… so then I need to make it loop through each path and match path1 to share1, path2 to share2 etc…
by Klaas at 2012-12-25 02:41:58
You can build a two dimensional array:
$mappings = @((‘Martin’,‘I:’),(‘Programmer’,‘J:’),(‘Media’,‘K:’))
$Server = '\server'
ForEach ($map in $mappings)
$share = $map[0]
$path = $map[1]
if(Test-Path -Path $path) {net use * /d /y}
net use $path $server+$share /user:$user $password

I didn’t check the net use syntax, so that may need some tuning.
This isn’t the only solution. You could use a hash instead of the array, you could write a function,… This is just the way I can explain fastest now.
by Symbiot at 2012-12-25 03:29:04
the foreach loop maps $map[0] to map[1] for each loop… shouldn’t it be progressing from 0…2 and 1…3?
by Klaas at 2012-12-26 00:22:51
No. the index [0] and [1] are applied to each $map. Actually, you could use $mappings[0][0] and [0][1] for the first one, [1][0] and [1][1] for the second pair, etc. But the foreach replaces the first index, so what remains is the first and second element of each pair, so [0] and [1] for each iteration.
In a hash table that would be ‘key’ and ‘value’ for each pair.