Powershelling the Kernel32 function GetComputerName

I was writing a blogpost on different ways of accessing the computer name running the script. So I thought well the old way of accessing it through Kernel32.dll should be possible. After looking through how to do it a lot I created a function that returns the computername or $Null. According to P/invoke one of the definitions of GetComputerName is as follows.

The function creates a method definition and then uses Add-Type to create a System.Runtime object. Since I choose the Stringbuilder version I needed to create a stringbuilder and a integer to pass to the function. Well this is the script as it ended up.

When it runs on my computer, it returns the computername correct.

Trackbacks

  1. […] wrote another post about this because it got a bit bigger than this small area. But the short story, could make the call […]

Leave a Reply