gpt4 book ai didi

powershell - Remove-PSDrive 不会删除驱动器

转载 作者:行者123 更新时间:2023-12-04 01:51:07 25 4
gpt4 key购买 nike

我使用 Powershell > 4 在我的计算机上创建和删除驱动器。我将它们本地连接到文件夹或远程驱动器:

New-PSDrive -Name L -PSProvider FileSystem -Root ($userprofile + "\Documents\whatever") -Scope Global -Persist
New-PSDrive -Name I -PSProvider FileSystem -Root \\server\whatever -Scope Global -Persist -Credential $usercred

现在我想更改驱动器并通过以下方式断开它们:
Get-PSDrive -Name L, I -ErrorAction SilentlyContinue | Remove-PSDrive -Scope Global -Force

如果没有 -ErrorAction,我会收到当前无法访问的网络驱动器的以下消息:
Get-PSDrive : The drive was not found. A drive with the name "I" does not exist.
In Zeile:1 Zeichen:1
+ Get-PSDrive -Name L, I -Scope ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (I:String) [Get-PSDrive], DriveNotFoundException
+ FullyQualifiedErrorId : GetDriveNoMatchingDrive,Microsoft.PowerShell.Commands.GetPSDriveCommand

不幸的是,所有驱动器(包括 L)都没有移除或断开连接。我通过 net use 检查并得到:
Getrennt   I:   \\Server\whatever                    Microsoft Windows Network
OK L: \\localhost\C$\...\Documents\wrong Microsoft Windows Network

你知道为什么 Remove-PSDrive 没有完成它的工作吗?

最佳答案

我遇到了与 Remove-PSDrive 相同的问题,没有删除任何网络驱动器,也没有输出任何错误,所以我通过使用 powershell 和旧的“网络使用”的组合解决了这个问题

$psdrive = psdrive | Where { $_.DisplayRoot -like '\\*' }
foreach ($mapdrive in $psdrive){

if($mapdrive.DisplayRoot -Like '\\NetworkPath\folder\etc*'){

$driveLetter = ($mapdrive.Root) -replace "\\",""
$drivePath = $mapdrive.DisplayRoot
Write-Host "Removing drive $driveLetter with path $drivePath" -foregroundcolor green

net use $driveLetter /delete

}
}

关于powershell - Remove-PSDrive 不会删除驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41035976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com