-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-drives.ps1
80 lines (74 loc) · 5.46 KB
/
map-drives.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Network drives
# Name = (Prefered letter, Path, (Alternate paths - will not map if already mapped on one of these))
$netdrvs = @{
"Files" = ("F:", "\\server.local\Files", @("\\server\Files", "\\10.0.0.1\Files"));
"Backup" = ("B:", "\\server.local\Backup", @("\\server\Backup", "\\10.0.0.1\Backup"))
}
# Drives for each group
# (Group, Drive Name)
$grpdrvs = @(
("domain.local\shares_files", "Files"),
("domain.local\shares_backup", "Backup")
)
# Valid drive letters
$letters = @("A:", "B:", "C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:", "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:")
# Get groups that current user is a member of
$headers = 0
$groups = @()
foreach ($i in $(whoami /GROUPS)) {
#Write-Output "LINE: $i"
if ($headers -eq 1) {
$x = $i -split " "
$groups += $x[0]
}
elseif ($i -Like "===*") {
$headers = 1
}
}
# Get list of already mounted shares
$sharesmounted = @()
foreach ($i in $(net use)) {
$x = $i -split " "
foreach ($j in $x) {
if ($j -Like "\\*") {
$sharesmounted += $j.Replace(" Microsoft Windows Network","").Trim()
}
}
}
# Get used drive letters
$drives = @()
foreach ($i in Get-PSDrive | Where-Object { $_.Provider -like "Microsoft.PowerShell.Core\FileSystem" } | Select-Object Name) {
$drives += $i.Name+":"
}
# Mount drives
foreach ($gd in $grpdrvs) {
# Are we in this group?
if ($groups -contains $gd[0]) {
# Is it currently not mapped?
$mappath = $netdrvs[$gd[1]][1]
$altpaths = $netdrvs[$gd[1]][2]
if ($sharesmounted -notcontains $mappath -and (Compare-Object $sharesmounted $altpaths -IncludeEqual -ExcludeDifferent -passthru).length -lt 1) {
$mapdrive = $netdrvs[$gd[1]][0]
# Is the prefered letter already in use?
$looped = 0
while ($drives -contains $mapdrive) {
# Get another free drive letter
$li = [array]::IndexOf($letters, $mapdrive)+1
if ($looped -eq 1 -and $li -ge [array]::IndexOf($letters,$netdrvs[$gd[1]][0])) {
write-output "Out of available drive letters. Aborting..."
exit
} elseif ($li -ge $letters.length) {
$mapdrive = $letters[0]
$looped = 1
} else {
$mapdrive = $letters[$li]
}
}
# Map the drive
write-output "Mapping $mapdrive -> $mappath ..."
net use $mapdrive "$mappath"
$sharesmounted += $mappath
$drives += $mapdrive
}
}
}