In powershell, how to check drive letter mapping and replace it only if it doesn't match the path, I don't want to delete it and add it again because this could kill other processes that are using the drive letter, but I don't mind checking it everytime.
Set-MapDrive "Z:" "//MyServer/Stuff1"
Here's what I have so far.
How to implement the Get-DriveMap Function?
function Set-MapDrive {
param(
[string]$letter,
[string]$shar_path
)
$curr_path = Get-DriveMap($letter)
# Letter Not Mapped
if ($curr_path -eq $null) {
net use $letter $share_path
}
else {
$dir1 = Get-Item $user_path
$dir2 = Get-Item $share_path
# Letter Map has changed
if($dir1.GetHashCode() -eq $dir2.GetHashCode()) {
net use $letter \delete
net use $letter $share_path
}
# No Change
else {
write-host "Note: Driver $letter already mapped to $share_path"
}
}
}
function Get-DriveMap {
param(
[string]$letter
)
$x = Get-PSDrive $letter
#^^^^ This produces error if letter doesn't exist?!
# need it to set $x to null if it doesn't exist.
return $x.DisplayRoot
}