在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
}
uj5u.com熱心網友回復:
使用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
}
uj5u.com熱心網友回復:
function my_drive_map {
param(
[string]$letter,
[string]$share_path
)
#example: my_map_drive "Z:" "\\MyServer\MyDir"
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"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327024.html
標籤:电源外壳
下一篇:位置引數有問題
