I need to restart the graphics driver automated on computers which users should not have admin privileges. My options to archive this is using powershell or C# code called from powershell.
The simplest way to restart the graphics driver is mentioned in this Question: PowerShell disable and enable a driver
$d = Get-PnpDevice| where {$_.class -like "Display*"}
$d | Disable-PnpDevice -Confirm:$false
$d | Enable-PnpDevice -Confirm:$false
But I cant use it because it needs admin privileges on executing. The Second and better approach is to use the win + ctrl + shift + b Hotkey which doesnt need admin rights. I have found a good example how to press the win key in a combination on this site: https://github.com/stefanstranger/PowerShell/blob/master/WinKeys.ps1
And builded it to my needs.
My actually code is this:
$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
If I try other combinations beside like win + e for opening the explorer it just works fine. But if i want to type win + ctrl + shift + b in, it just do nothing and closes itself without a error message.
Am I missing something?