您可以通過使用.NET Framework中TerminalServerSession布爾屬性做到這一點最簡單的方法:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SystemInformation]::TerminalServerSession
輸出:
False
或者你可以做手工的方式(如TerminalServerSession
在內部完成)並使用C#和P/Invoke添加負載並在PowerShell中使用GetSystemMetrics()
。
$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)
輸出:
0
我試過以下(Win7的),我得到一個組件缺失。我要輸入什麼? PS C:\> [System.Windows.Forms.SystemInformation] :: TerminalServerSession 無法找到[System.Windows.Forms.SystemInformation]類型。確保包含此類型的程序集已加載。 - 找到了! Add-Type -Assembly System.Windows.Forms – EdLipson 2015-02-23 17:41:13
是的,通常我會加入它,但是在Win10上,它已經因爲某種原因被加載了,所以我不認爲這個類是必需的。現在更新答案。 :) – 2015-02-23 17:46:36