Skip to content

Instantly share code, notes, and snippets.

@Hexalon Hexalon/Lock-Computer.ps1
Last active Mar 28, 2016

Embed
What would you like to do?
Uses C# to call an Win32 API to lock the computer
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.117
Created on: 3/25/2016 17:33
Created by: Colin Squier <hexalon@gmail.com>
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
You can’t perform that action at this time.