如何使用 PowerShell 以编程方式弹出锁定的 HDD eSATA 驱动器?

问题描述 投票:0回答:1

我试图通过“Windows 的 USB 托盘图标”弹出 eSATA HDD 驱动器以安全删除,但这是不可能的,因为“另一个程序正在使用该驱动器”错误(我已经厌倦了这个错误@# Windows 错误)。

我在 PowerShell 上试过这段代码:

$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq 'F:\'}  
$vol.DriveLetter = $null  
$vol.Put()  
$vol.Dismount($false, $false)

还有这个:

$Eject = New-Object -comObject Shell.Application
$Eject.NameSpace(17).ParseName($usbDrvLetter+“:”).InvokeVerb(“Eject”) 

什么也没有发生。

有什么有效的方法可以让它工作吗?

windows powershell drive
1个回答
1
投票

不确定,但

.Dismount()
方法的重载定义是:

OverloadDefinitions
-------------------
System.Management.ManagementBaseObject Dismount(System.Boolean Force, System.Boolean Permanent)

所以也许改为:

$Vol.Dismount($true, $true)
有效地强制下马。

我试过测试这个,除了一般我认为你应该使用 CIM cmdlet 而不是 -WMI

Get-WMIObject
在 Windows PowerShell 中已弃用,并已从 PowerShell Core 中删除。

考虑 Dismount 方法的潜在返回码(记录在这里)也很重要。

RETURN VALUE   Return code  Description
------------   ------------------------
0              Success
1              Access Denied
2              Volume Has Mount Points
3              Volume Does Not Support The No-Autoremount State
4              Force Option Required

获取卷实例:

$Vol = Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = 'E:'"

这显然与旧方法非常相似。然而,调用方法有点不同:

$Vol | Invoke-CimMethod -MethodName Dismount -Arguments @{ Force = $true; Permanent = $true }

ReturnValue PSComputerName
----------- --------------
      2

而且,它似乎没有卸载驱动器。但是,如果我更改参数:

$Vol | Invoke-CimMethod -MethodName Dismount -Arguments @{ Force = $true; Permanent = $false }
ReturnValue PSComputerName
----------- --------------
          0

这似乎确实卸载了驱动器,因为资源管理器转移了焦点。但是,驱动器仍然可见,单击时可以访问。我从来没有看到安全弹出消息。似乎卸载驱动器并不等同于安全弹出它。

© www.soinside.com 2019 - 2024. All rights reserved.