英文:
Disabling a display adapter device via powershell is returning error
问题
目标
通过PowerShell禁用和重新启用显示适配器,以使其在Windows休眠后重新工作。
场景描述
我的GPU卡位于通过Thunderbolt连接到我的笔记本电脑的外部GPU封装中。
当笔记本电脑从休眠中醒来时,连接到eGPU的所有外设(鼠标、键盘、网络摄像头等)都工作正常,但GPU卡不工作。
我认为这与Windows休眠时的节能设置以及eGPU处理GPU卡的方式有关,但直到最近,我找不到其他解决方案,只能重新启动机器。
上周我找到了一个解决办法。我可以手动在设备管理器中禁用然后重新启用显示适配器。我每天这样做约5-6次,比重新启动整个机器并等待它重新打开我的所有应用程序和浏览器标签要好得多,因为我是开发人员,所以我觉得这是一个很好的机会来学习更多关于PowerShell的知识
设备类别GUID
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
Device Class Guid
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论