Powershell 移除PSDrive不移除驱动器
我使用Powershell>4在计算机上创建和删除驱动器。我将它们本地连接到文件夹或远程驱动器:Powershell 移除PSDrive不移除驱动器,powershell,Powershell,我使用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
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和传统的“网络使用”解决了这个问题
对我来说,更整洁的解决方案是使用Remove SmbMapping和New SmbMapping。此代码按预期工作:
Function New-MappedDrive ([Char]$Letter, [string]$Path){
If (-not (Test-Path -Path $Path)) {
Write-Host " Unable to map drive '"$Letter"' - Invalid path" -ForegroundColor Red
}
else{
#Remove existing connections
if ("$($Letter):" -in (Get-SmbMapping).LocalPath){Remove-SmbMapping -LocalPath $Letter":" -Force | Out-Null}
New-SmbMapping -LocalPath $Letter":" -RemotePath $Path -Persistent $true | Out-Null
}
}
试试这个方法。我也无法单独使用下面的命令来实现这一点,但它最终正确地清除了映射。我认为它可能与映射驱动器时的“Persist”参数有关(但根据MS文档,在删除驱动器时这并不重要)。我使用了Try..Catch块(不是最优雅的),但是如果您在驱动器被移除后再次尝试运行它,则此方法会抱怨驱动器不存在
try
{
$mappings_to_remove = Get-PSDrive L, I -ErrorAction SilentlyContinue
Remove-PSDrive $mappings_to_remove -PSProvider FileSystem -Scope Global -erroraction SilentlyContinue | Out-Null
Remove-SMBMapping $mappings_to_remove -Force -erroraction SilentlyContinue | Out-Null
}
catch
{
}
在WinOS中装载共享有多种方法—利用
获取PSDrive
、获取SmbMapping
、获取SmbGlobalMapping
、网络使用
等来列出现有共享。确保以管理员身份运行powershell.exe,并运行与装载创建方式相关联的removla命令。您无需触摸注册表来查找装入点2
——这是一种黑客行为
删除全局SMB共享
删除SMB共享
删除PS共享
净使用
-错误操作一直持续
,你说你看不出它为什么不工作?如果你删除它,你会出错吗?我试过了,结果出错了。不要问我为什么没有把它添加到我的问题中。我编辑了这个问题。英文错误:“找不到驱动器。不存在名为“I”的驱动器。它仅与L驱动器一起工作吗?我没有收到L驱动器的错误消息。但是,它仍然是可以到达的。这是一种可能性。然而,我感到惊讶的是,这不适用于PS本身。在PowerShell命令启动进程-FilePath“net”-ArgumentList”中使用net use的一行程序使用$((Get WmiObject-Class Win32\u mappedLogicalDisk-filter“ProviderName='\\\\NetworkPath\\folder\\etc'))。Name.SubString(0,1)):/delete“
这看起来很有趣。帮助只显示了我的密码可能是明文的。是否有新PSDrive中类似的-Credential
选项可用?
try
{
$mappings_to_remove = Get-PSDrive L, I -ErrorAction SilentlyContinue
Remove-PSDrive $mappings_to_remove -PSProvider FileSystem -Scope Global -erroraction SilentlyContinue | Out-Null
Remove-SMBMapping $mappings_to_remove -Force -erroraction SilentlyContinue | Out-Null
}
catch
{
}
Remove-SmbGlobalMapping K:
Remove-SmbMapping K:
Remove-PSDrive K
net use K: /delete