简体   繁体   English

如何使用 powershell 在远程计算机上获取 Caps Lock 状态

[英]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时,该命令有效。

Using [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。

Using MS Word使用 MS Word

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

Using the 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' )

Using the Win32 API使用Win32 API

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 . 0x14CapsLock的密钥 ID,而0x90NumLock的密钥 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 本身的文档的此页面

Doing it remotely远程进行

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.

相关问题 如何使用PowerShell获取远程计算机上正在运行的应用程序的列表? - How to get list of running applications on remote computer using PowerShell? 如何使用 PowerShell 获取远程计算机的可用物理内存 - How to get free physical memory of remote computer using PowerShell 如何在Powershell中刷新远程计算机上的服务状态? - How do I refresh service status on remote computer in powershell? Powershell:获取远程计算机名称 - Powershell : Get remote computer name 使用 Powershell 从远程计算机获取从服务器安装的打印机 - Get printers installed from server from remote computer using Powershell Powershell使用psexec和oneliner在远程计算机上获取文件夹大小并进行排序 - Powershell get folders sizes with sort on remote computer using psexec and oneliner 如何使用带有Powershell的凭据获取远程计算机的每个子目录的大小? - How can I get the size of each sub-directory of a remote computer using credentials with powershell? 如何使用Powershell远程到计算机本身 - How to remote to the computer itself with powershell 如何通过Powershell获取所有远程计算机的用户名? - How to get all remote computer's usernames via Powershell? 如何使用PowerShell检查特定服务是否在远程计算机上运行 - How to check if a particular service is running in a remote computer using PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM