[英]How to get Caps Lock status on remote computer using powershell
I have tried the following command but I only get a value of False
:我尝试了以下命令,但我只得到False
的值:
Invoke-Command -ComputerName Server01 -ScriptBlock {[console]::CapsLock}
When I run [console]::CapsLock
locally the command works.当我在本地运行[console]::CapsLock
时,该命令有效。
[Console]::CapsLock
使用[Console]::CapsLock
The [Console]::CapsLock
property can be read to get whether CapsLock
is On or Off ( $true
or $false
):可以读取[Console]::CapsLock
属性以获取CapsLock
是打开还是关闭( $true
或$false
):
[Console]::CapsLock
You can also check the state of NumLock
with [Console]::NumberLock
as well.您还可以使用[Console]::NumberLock
检查NumLock
的 state。
Seems that the remote session doesn't reflect whether CapsLock
is on or off based on whether the physical keyboard has it on or off.似乎远程 session 不能根据物理键盘是否打开或关闭来反映CapsLock
是打开还是关闭。
I was able to find another method but don't have a way to test it myself, and it requires Microsoft Word to be installed:我能够找到另一种方法,但没有办法自己测试它,它需要安装 Microsoft Word:
$word = New-Object -ComObject "Word.Application"
# Check CapsLock
$word.CapsLock
# Check NumLock
$word.NumLock
System.Windows.Forms
Namespace使用System.Windows.Forms
命名空间A third method that might work for your needs would be to use the System.Windows.Forms
namespace to read the caps lock setting.第三种可能满足您需求的方法是使用System.Windows.Forms
命名空间来读取大写锁定设置。
# Make System.Windows.Forms available in Powershell
Add-Type -AssemblyName System.Windows.Forms
# Check CapsLock
[System.Windows.Forms.Control]::IsKeyLocked( 'CapsLock' )
# Check NumLock
[System.Windows.Forms.Control]::IsKeyLocked( 'NumLock' )
You can also check the state using the Win32 API:您还可以使用 Win32 API 检查 state:
# Compile some quick C# so we can `P/Invoke` to the native library
$signature = @"
[DllImport("USER32.dll")]
public static extern short GetKeyState(int nVirtKey);
"@
$Kernel32 = Add-Type -MemberDefinition $signature -Name Kernel32 -Namespace Win32 -Passthru
# Check CapsLock
[bool]( $Kernel32::GetKeyState(0x14) )
# Check NumLock
[bool]( $Kernel32::GetKeyState(0x90) )
0x14
is the key id for CapsLock
and 0x90
is the key id for NumLock
. 0x14
是CapsLock
的密钥 ID,而0x90
是NumLock
的密钥 ID。 See this page for more information on making use of this API from .NET , and this page for the documentation of GetKeyState itself .有关从 .NET 使用此 API 的更多信息,请参阅此页面,以及有关 GetKeyState 本身的文档的此页面。
I could not find any reliable methods of doing this remotely, which makes sense, as CapsLock
is a per session setting - not a system wide one.我找不到任何可靠的远程执行此操作的方法,这是有道理的,因为CapsLock
是每个 session 设置 - 不是系统范围的设置。 Even assuming there is a way to get the CapsLock
status for a given active session, you could have several user sessions on one remote system at once, and it becomes difficult to know which session to look at for its CapsLock
status.即使假设有一种方法可以获取给定活动 session 的CapsLock
状态,您也可以同时在一个远程系统上拥有多个用户会话,并且很难知道要查看哪个 session 的CapsLock
状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.