1

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?

Oliver
  • 93
  • 1
  • 7
  • I copied/pasted your code and removed the extra indentation (because here-strings don't like indentation at `"@`) and your code works as expected for myself in both PowerShell V5 and Powershell ISE. Can you run it in Powershell ISE as it won't close after running to see if there is any output. – Jacob Aug 20 '19 at 18:22
  • @Jacob Thank you for testing it. My PSVersion is "5.1.17763.316" and if I run it with Powershell ISE it executes without an Error, but dont restart my graphics driver as expected. And its really successfully restarting your Graphics driver per shortcut? The screen should be dark for one to three seconds. – Oliver Aug 21 '19 at 08:03
  • Yes, works as expected on a Surface Pro 3, PSVersion 5.1.18362.145. Although it's worth noting I'm logged onto an account with Administrator privileges, I'll try a test account without admin privileged and see if there is any change. – Jacob Aug 25 '19 at 20:28

1 Answers1

1

For future readers.The code works fine, but there are group policies in my company AD which make the execution fail.

Anyway, my supervisor has decided on a different way than testing hundreds of group policies for the error.

Oliver
  • 93
  • 1
  • 7