检查驱动器号映射,仅在不匹配时才替换它(Powershell)



在powershell中,如何检查驱动器号映射并仅在路径不匹配时替换它,我不想删除它并再次添加它,因为这可能会杀死正在使用驱动器号的其他进程,但我不介意每次检查它。

Set-MapDrive "Z:" "//MyServer/Stuff1"

这是我到目前为止写的。

如何实现Get-DriveMap函数?

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
}

使用Get-PSDrive获取任何现有驱动器:

function Set-MapDrive {
param(
[string]$Letter,
[string]$RootPath
)
$existingDrive = Get-PSDrive -Name $Letter.TrimEnd(':') -ErrorAction SilentlyContinue
if($existingDrive){
if($existingDrive.Root -eq $RootPath){
# nothing more to do, drive already exists with correct root path
return
}
# remove existing drive mapping here
}
# create new drive mapping here
}
function my_drive_map {
param(
[string]$letter, 
[string]$share_path
)
#example: my_map_drive "Z:" "\MyServerMyDir"
write-host "my_drive_map $letter $share_path"
$L  = $letter.TrimEnd(':')
$LL = "${L}:"
if (-not (Test-Path $share_path)) {
write-host "    ERROR: Network share path is inaccessable"
exit 1
}
$map = Get-SmbMapping -LocalPath $LL -ErrorAction SilentlyContinue  

#$map | select *
if ($map -eq $null) {
write-host "    # case1: Drive Not Mapped Yet"
write-host "    PS> Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue (case3)"
Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null
write-host "    PS> New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent"
New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent $true | out-null
}

# Drive-Letter Already Mapped
else  {    
$dir1 = $map.RemotePath
$dir2 = $share_path

# Drive-Letter Map has changed, Remap it
if($dir1 -ne $dir2) {
write-host "    # case2: Drive-Letter already mapped to wrong Path, Change it"
write-host "    # comparing path1($dir1) to path2($dir2)"

write-host "    PS> Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null"
Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null        
write-host "    PS> New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent"
New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent $true | out-null
}
else {
write-host "    # case3: Drive-Letter already mapped and is correct"
write-host "    # comparing path1($dir1) to path2($dir2)"
}
}       

# Check that Drive-Letter is available
if (-not (Test-Path "${L}:")) {
write-host "    ERROR: Drive-Letter ${L}: is still inaccessable"
exit 1
}

write-host  "    (OK) ${L}: accessable as $share_path"
}
function my_drive_list {
Get-SmbMapping
}
function my_drive_remove {
param(
[string]$letter
)
write-host "my_drive_remove $letter $share_path"
$L  = $letter.TrimEnd(':')
$LL = "${L}:"
Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null

$map = Get-SmbMapping -LocalPath $LL -ErrorAction SilentlyContinue  

if ($map -eq $null) {
write-host "    (OK) Drive sucessfully removed"
}
else {
write-host "    (err) Drive still attached"
}

}