禁用显示适配器设备通过PowerShell返回错误。

huangapple go评论70阅读模式
英文:

Disabling a display adapter device via powershell is returning error

问题

目标

通过PowerShell禁用和重新启用显示适配器,以使其在Windows休眠后重新工作。

场景描述

我的GPU卡位于通过Thunderbolt连接到我的笔记本电脑的外部GPU封装中。
当笔记本电脑从休眠中醒来时,连接到eGPU的所有外设(鼠标、键盘、网络摄像头等)都工作正常,但GPU卡不工作。

我认为这与Windows休眠时的节能设置以及eGPU处理GPU卡的方式有关,但直到最近,我找不到其他解决方案,只能重新启动机器。

上周我找到了一个解决办法。我可以手动在设备管理器中禁用然后重新启用显示适配器。我每天这样做约5-6次,比重新启动整个机器并等待它重新打开我的所有应用程序和浏览器标签要好得多,因为我是开发人员,所以我觉得这是一个很好的机会来学习更多关于PowerShell的知识 禁用显示适配器设备通过PowerShell返回错误。

设备类别GUID

禁用显示适配器设备通过PowerShell返回错误。

PowerShell脚本

# 获取显示适配器
$adapter = Get-PnpDevice -FriendlyName "NVIDIA GeForce RTX 3070" | Where-Object {$_.ClassGuid -eq "{4d36e968-e325-11ce-bfc1-08002be10318}"}

# 禁用显示适配器
Disable-PnpDevice -InstanceId $adapter.InstanceId

# 等待5秒
Start-Sleep -Seconds 5

# 启用显示适配器
Enable-PnpDevice -InstanceId $adapter.InstanceId

返回的错误

在目标 "Win32_PnPEntity: NVIDIA GeForce RTX 3070 (DeviceID = PCI\VEN_10DE&DEV_2488&SUBSYS_138A196E&R...) 的操作 "禁用" 上执行。
Disable-PnpDevice:通用故障
位于 C:\Users\me\Desktop\NVIDIA_Reboot.ps1:5 的字符:1
+ Disable-PnpDevice -InstanceId $adapter.InstanceId
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:(未指定:[Win32_PnPEntity..._138A196E&R...]:ROOT\cimv2\Win32_PnPEntity)[Disable-Pn
pDevice],CimException
+ FullyQualifiedErrorId:HRESULT 0x80041001,Disable-PnpDevice

英文:

Objective

Disable and re-enable a display adapter via Powershell to make it work again after Windows puts it to sleep.

Scenario description

My GPU card sits on an eGPU enclosure connected via Thunderbolt to my laptop.
When the laptop awakes from sleep, all the peripherals connected to the eGPU work well (mouse, keyboard, webcam, etc) but the GPU card doesn't.

I'm assuming this has to do with the power saving settings when Windows goes to sleep and the way the eGPU handles the GPU card but until recently I found no other solution rather than rebooting the machine.

Last week I found a work around. I can manually disable and then re-enable the display adapter in the device manager. I do this about 5-6 times a day and it's much better than rebooting the whole machine and waiting for it to reopen all my apps and browser tabs for my dev work.

So then I thought, instead of manually opening the device manager, what if I created a powershell script for this? This obviously is not life and death but I'm a developer and curious, so I thought this would be a great opportunity to learn a bit more of powershell 禁用显示适配器设备通过PowerShell返回错误。

Device Class Guid

禁用显示适配器设备通过PowerShell返回错误。

Powershell script

# Get the display adapter
$adapter = Get-PnpDevice -FriendlyName "NVIDIA GeForce RTX 3070" | Where-Object {$_.ClassGuid -eq "{4d36e968-e325-11ce-bfc1-08002be10318}"}

# Disable the display adapter
Disable-PnpDevice -InstanceId $adapter.InstanceId

# Wait for 5 seconds
Start-Sleep -Seconds 5

# Enable the display adapter
Enable-PnpDevice -InstanceId $adapter.InstanceId

Error returned

Performing the operation "Disable" on target "Win32_PnPEntity: NVIDIA GeForce RTX 3070 (DeviceID =                      "PCI\VEN_10DE&DEV_2488&SUBSYS_138A196E&R...)".                                                                          
Disable-PnpDevice : Generic failure
At C:\Users\me\Desktop\NVIDIA_Reboot.ps1:5 char:1
+ Disable-PnpDevice -InstanceId $adapter.InstanceId
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Win32_PnPEntity..._138A196E&R...):ROOT\cimv2\Win32_PnPEntity) [Disable-Pn
   pDevice], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041001,Disable-PnpDevice

答案1

得分: 1

Solution: 以管理员身份运行脚本 (doh)

"CimException" 中的错误描述提供了线索,此 Stack Overflow 文章 解释了上下文。

英文:

Solution: run the script as an Admin (doh)

"CimException" on the error descriptions gives the clue, and this article on Stack Overflow explains the context

huangapple
  • 本文由 发表于 2023年2月24日 08:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定