【问题标题】:How to programmatically eject locked HDD eSATA drive with PowerShell?如何使用 PowerShell 以编程方式弹出锁定的 HDD eSATA 驱动器?
【发布时间】:2021-02-28 13:44:41
【问题描述】:

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

什么也没发生。

任何有效的方法可以让它工作?

【问题讨论】:

  • 您不需要将卸载设置为$true吗?

标签: windows powershell drive programmatically


【解决方案1】:

不确定,但.Dismount() 方法的重载定义是:

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

所以也许改成:$Vol.Dismount($true, $true) 有效地强制下马。

我已尝试对此进行测试,但通常我认为您应该使用 CIM cmdlet 而不是 -WMIGet-WMIObject 在 Windows PowerShell 中已弃用,并已从 PowerShell Core 中删除。

考虑 Dismount 方法的潜在返回码 (documented here) 也很重要。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2010-11-29
    • 2011-05-25
    • 2021-04-15
    相关资源
    最近更新 更多