如何在没有管理员权限的情况下使用 Powershell 或 C# 重新启动图形驱动程序



我需要重新启动用户不应具有管理员权限的计算机上自动的图形驱动程序。我存档此的选项是使用从 powershell 调用的 powershell 或 C# 代码。

此问题中提到了重新启动图形驱动程序的最简单方法: PowerShell 禁用和启用驱动程序

$d = Get-PnpDevice| where {$_.class -like "Display*"}
$d  | Disable-PnpDevice -Confirm:$false
$d  | Enable-PnpDevice -Confirm:$false

但我不能使用它,因为它需要执行时的管理员权限。 第二种也是更好的方法是使用不需要管理员权限的 win + ctrl + shift + b 热键。 我在这个网站上找到了一个很好的例子,如何组合按下win键:https://github.com/stefanstranger/PowerShell/blob/master/WinKeys.ps1

并根据我的需求构建它。

我的实际代码是这样的:

$source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyboardSend
{
public class KeyboardSend
{
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
public static void KeyDown(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
"@
Add-Type -TypeDefinition $source -ReferencedAssemblies "System.Windows.Forms"
Function Win($Key, $Key2, $Key3)
{
[KeyboardSend.KeyboardSend]::KeyDown("LWin")
[KeyboardSend.KeyboardSend]::KeyDown("$Key")
[KeyboardSend.KeyboardSend]::KeyDown("$Key2")
[KeyboardSend.KeyboardSend]::KeyDown("$Key3")
[KeyboardSend.KeyboardSend]::KeyUp("LWin")
[KeyboardSend.KeyboardSend]::KeyUp("$Key")
[KeyboardSend.KeyboardSend]::KeyUp("$Key2")
[KeyboardSend.KeyboardSend]::KeyUp("$Key3")
}
Win 163 161 66
# 163 = ctrl key
# 161 = shift key
# 66 = b key

如果我尝试其他组合,例如 win + e 来打开资源管理器,它就可以正常工作。但是,如果我想输入 win + ctrl + shift + b,它什么都不做,并且会自行关闭而不会显示错误消息。

我错过了什么吗?

供未来的读者使用。代码工作正常,但我的公司 AD 中有组策略使执行失败。

无论如何,我的主管已经决定采用与测试数百个组策略不同的方法

最新更新