如何使用 powershell 在远程计算机上获取 Caps Lock 状态 [英] How to get Caps Lock status on remote computer using powershell

查看:122
本文介绍了如何使用 powershell 在远程计算机上获取 Caps Lock 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下命令,但我只得到 False 的值:

I have tried the following command but I only get a value of False:

Invoke-Command -ComputerName Server01  -ScriptBlock {[console]::CapsLock} 

当我在本地运行 [console]::CapsLock 时,该命令有效.

When I run [console]::CapsLock locally the command works.

推荐答案

Using[Console]::CapsLock

[Console]::CapsLock 属性可以被读取以获取 CapsLock 是 On 还是 Off($true$false):

Using[Console]::CapsLock

The [Console]::CapsLock property can be read to get whether CapsLock is On or Off ($true or $false):

[Console]::CapsLock

您也可以使用 [Console]::NumberLock 来检查 NumLock 的状态.

You can also check the state of NumLock with [Console]::NumberLock as well.

似乎远程会话并没有根据物理键盘是否打开或关闭CapsLock 来反映它是打开还是关闭.

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.

我找到了另一种方法,但没有办法自己测试,它需要安装 Microsoft Word:

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:

$word = New-Object -ComObject "Word.Application"

# Check CapsLock
$word.CapsLock

# Check NumLock
$word.NumLock

使用 System.Windows.Forms 命名空间

可能满足您需求的第三种方法是使用 System.Windows.Forms 命名空间来读取大写锁定设置.

Using the System.Windows.Forms Namespace

A third method that might work for your needs would be to use the System.Windows.Forms namespace to read the caps lock setting.

# 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' )

使用 Win32 API

您还可以使用 Win32 API 检查状态:

Using the Win32 API

You can also check the state using the Win32 API:

# 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) )

0x14CapsLock 的密钥 ID,0x90NumLock 的密钥 ID.有关从 .NET 使用此 API 的更多信息,请参阅此页面,和 该页面获取 GetKeyState 本身的文档.

0x14 is the key id for CapsLock and 0x90 is the key id for NumLock. See this page for more information on making use of this API from .NET, and this page for the documentation of GetKeyState itself.

我找不到任何可靠的远程执行此操作的方法,这是有道理的,因为 CapsLock 是针对每个会话的设置 - 而不是系统范围的设置.即使假设有一种方法可以获取给定活动会话的 CapsLock 状态,您也可以同时在一个远程系统上拥有多个用户会话,并且很难知道要查看哪个会话CapsLock 状态.

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. 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.

这篇关于如何使用 powershell 在远程计算机上获取 Caps Lock 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆