My First PowerShell Pinvoke –Lock-MyWorkstation


Yesterday I watched the amazing webcast from PowerShell Jedi Adam Driscoll about the fantastic world of Win32 Subsystem and ..oh boy.. I got a new purpose in my life LOL.

Adam showed to me a new world, full of possibilities and challengers. To start my day (a few hours before start my trip to USA and Summit 2013), I tried my first code : A just lock workstation using Win32 interop.I know, it is simple and ridiculous , but it is my first stuff ever with that, so I am proud of myself 🙂

I am a novice in C# , C++ and whatever  , so with some help of another Jedi, Mladen Prajdic that helped me , after almost 2 hours trying, he showed me what was going wrong. I was defining the class Public with P in uppercase and it is in lowercase.

Crap..but shit happens.

Also I had some ideas from the another Jedi (yeahh I am claiming for the Force with the Jedi Council)  Trevor Sullivan –> PowerShell: Disable CapsLock , my final code is :

 

Function Lock-MyWorkstation {
    $PoshLockWorkStation = @”

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

public static class PoshLockWorkStation
{
    [DllImport(“user32.dll”, SetLastError = true)]
        static extern bool LockWorkStation();
    public static void LockItPlease()
    {
        bool result = LockWorkStation();
        if( result == false )
        {
            throw new Win32Exception( Marshal.GetLastWin32Error() );
        }
    }
}
“@
    if (-not $PoshLockWorkStation) {
        Add-Type $PoshLockWorkStation  -ReferencedAssemblies System.Runtime.InteropServices
    }

    [PoshLockWorkStation]::LockItPlease()
}

Lock-MyWorkstation

 

HA..and it worked !!!! .

I just ordered some books. Windows Internals 6th Part 1 and 2, PowerShell Deep Dives and in the process of get a pluralsight account to learn C#.

It seems that, as Adam said, my end of the year will be with pain, but a good and pleasurable pain

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Algo que Esqueci de Categorizar. Bookmark the permalink.

2 Responses to My First PowerShell Pinvoke –Lock-MyWorkstation

  1. Greg Wojan says:

    Laerte,

    I tried your code and couldn’t get it to work. After playing around a little changing the ‘Add-Type’ fragment to:

    try {
    Add-Type $PoshLockWorkStation
    }
    catch {}

    everything works like a charm. You don’t need the -ReferencedAssemblies parameter in this case and the pattern of using if(-not [type[) simply doesn’t work. I’ve seen it used before and it’s never worked for me. The try…catch{} block is much more reliable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s