Get system metrics using PowerShell
Question: I have a Python script which prints 1 if it is running under RDP or 0 if it is
not.

    
    
    from ctypes import *
    SM_REMOTESESSION = 0x1000
    print(windll.user32.GetSystemMetrics(SM_REMOTESESSION))
    

I'd like to get the same information using PowerShell. How do I do the
equivalent of `GetSystemMetrics` in PowerShell?


Answer: You can do it the easy way by using the
[TerminalServerSession](https://msdn.microsoft.com/en-
us/library/system.windows.forms.systeminformation.terminalserversession.aspx)
bool-property from .NET Framework:

    
    
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.SystemInformation]::TerminalServerSession
    

Output:

    
    
    False
    

Or you could do it the manual way (like `TerminalServerSession` does
internally) and use C# and P/Invoke to add load and use `GetSystemMetrics()`
in PowerShell.

    
    
    $def = @"
    //I removed every other enum-value to shorten the sample
    public enum SystemMetric
       {
         SM_REMOTESESSION           = 0x1000, // 0x1000
       }
    
    [DllImport("user32.dll")]
    public static extern int GetSystemMetrics(SystemMetric smIndex);
    "@
    
    Add-Type -Namespace NativeMethods -Name User32Dll -MemberDefinition $def
    
    [NativeMethods.User32Dll]::GetSystemMetrics([NativeMethods.User32Dll+SystemMetric]::SM_REMOTESESSION)
    

Output:

    
    
    0
    

