Last active
August 26, 2020 14:06
-
-
Save Hexalon/b535d1afa578e9c476ab to your computer and use it in GitHub Desktop.
Uses C# to call an Win32 API to lock the computer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.NOTES | |
=========================================================================== | |
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.117 | |
Created on: 3/25/2016 17:33 | |
Created by: Colin Squier <[email protected]> | |
Filename: Lock-Computer.ps1 | |
=========================================================================== | |
.DESCRIPTION | |
Uses C# to call an Win32 API to lock the computer | |
#> | |
$code = @" | |
using System; | |
using System.Runtime.InteropServices; | |
public static class LockDesktop | |
{ | |
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] | |
private static extern IntPtr GetDesktopWindow(); | |
[DllImport("user32.dll")] | |
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); | |
[DllImport("user32.dll", EntryPoint = "LockWorkStation")] | |
private static extern IntPtr LockWorkStation(); | |
private const int SC_SCREENSAVE = 0xF140; | |
private const int WM_SYSCOMMAND = 0x0112; | |
public static void SetScreenSaverRunning() | |
{ | |
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0); | |
LockWorkStation(); | |
} | |
} | |
"@ | |
Add-Type -TypeDefinition $code | |
[LockDesktop]::SetScreenSaverRunning() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment