I was writing a blog post 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.

[System.Runtime.InteropServices.DllImport("Kernel32", CharSet=CharSet.Auto)]
    public static extern bool GetComputerName(StringBuilder buffer, ref uint size);

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.

Function Get-Kernel32ComputerName{
#Building the definition, since Stringbuilder isnt part of system I needed to specify system.text.stringbuilder
$MethodDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetComputerName(System.Text.StringBuilder buffer, ref uint size);
'@
#Adding the definition to get the system runtime.
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
#Create a stringbuilder with the size to hold the maximum size netbios name plus a \0
$computername = New-Object System.Text.StringBuilder 64
#create the size variable with the capacity of the stringbuilder
$size=$computername.Capacity
#Execute the function and send along the computername and size variables.
if ($Kernel32::GetComputerName($computername,[ref] $size))
{
 #If the function succeed return the computername
 return $computername.toString()
}
#If the function fails return null
return $null
}
When it runs on my computer, it returns the computername correct.