[英]PowerShell is slow when i want to run a simple script via keyboard command/shortcut. Any way to let it run faster?
I don't have keys on my keyboard to adjust the brightness of my screen.我的键盘上没有用于调节屏幕亮度的按键。 So i made a simple script to increase or decrease the brightness by 10%:
所以我做了一个简单的脚本来增加或减少 10% 的亮度:
Brightness up: $Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness |调高亮度:$Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness | select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)
select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)
For reducing the brightness i replace the + by a -.为了降低亮度,我用 - 替换了 +。
I saved these scripts as ps1 files and made a shortcut on my desktop to open these in powershell via a keyboard command: Ctrl Shift + for increasing and Ctrl Shift - for decreasing the brightness.我将这些脚本保存为 ps1 文件,并在我的桌面上创建了一个快捷方式,通过键盘命令在 powershell 中打开这些脚本:Ctrl Shift + 用于增加亮度,Ctrl Shift - 用于降低亮度。
Now here's the problem: when i press the command on my keyboard it takes a while for powershell to start up and run the script.现在的问题是:当我按下键盘上的命令时,powershell 需要一段时间才能启动并运行脚本。 It takes about 5 seconds for my screen to actually change brightness.
我的屏幕实际改变亮度大约需要 5 秒。
Is there any way to adjust the script or something to let it run faster?有什么方法可以调整脚本或让它运行得更快吗? Just like it would on a normal keyboard shortcut.
就像在普通的键盘快捷键上一样。 Then it's almost instant.
然后它几乎是瞬间的。
I was thinking about already letting powershell run in the background so that it doensn't have to boot up first, but i have no idea how.我正在考虑让 powershell 在后台运行,这样它就不必先启动,但我不知道如何。
Thanks in advance.提前致谢。 Really curious if there's a way.
真的很好奇有没有办法。
What i did so far: $Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness |到目前为止我做了什么: $Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness | select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)
select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)
Saved this as ps1 in my documents file.在我的文档文件中将其保存为 ps1。
Made a shortcut on desktop linking to the file path and assigned Ctrl Shift + as a keyboard shortcut to run it in PowerShell.在桌面上创建链接到文件路径的快捷方式,并指定 Ctrl Shift + 作为键盘快捷方式以在 PowerShell 中运行它。
The startup cost of Windows PowerShell's CLI, powershell.exe
, is significant. Windows PowerShell 的 CLI
powershell.exe
的启动成本很高。
Even if you implement a custom PowerShell command server that keeps running and receives commands to execute through some IPC mechanism, you'll need some executable (and therefore launching of a new process ) that the shortcut file invokes, which must request the execution of the desired command from the command server.即使您实现了一个自定义的 PowerShell 命令服务器,该服务器保持运行并通过某种 IPC 机制接收要执行的命令,您仍需要快捷方式文件调用的一些可执行文件(并因此启动一个新进程),它必须请求执行来自命令服务器的所需命令。
Therefore:所以:
Using a shortcut-key mechanism that relies on shortcut files ( *.lnk
) is invariably slow (though with an executable that has a low startup cost it may be fast enough, depending on the use case).使用依赖于快捷方式文件 (
*.lnk
) 的快捷键机制总是很慢(尽管对于启动成本低的可执行文件,它可能足够快,具体取决于用例)。
Consider the following alternatives:考虑以下替代方案:
Use a dedicated scripting language such as AutoHotkey (AHK) , which offers fast, in-process shortcut-key commands based on a resident process.使用专用脚本语言,例如AutoHotkey (AHK) ,它提供基于常驻进程的快速进程内快捷键命令。
Use a dedicated utility for adjusting monitor brightness that also supports shortcut keys (hotkeys):使用也支持快捷键(热键)的专用实用程序来调整显示器亮度:
If you'd like to use PowerShell you need to keep a script running continuously (or at least the console for you to enter commands).如果您想使用 PowerShell,您需要保持脚本连续运行(或至少让您输入命令的控制台)。
If you don't like to have a console up and running all the time, you have to revert to some heavier methods involving running the script hidden and triggering on Windows key events or building a Systray tool with a menu...如果你不喜欢一直启动和运行控制台,你必须恢复到一些更重的方法,包括运行隐藏脚本和触发 Windows 键事件或构建一个带有菜单的系统托盘工具......
It's quite possible, but involves a lot more coding then you might be prepared to do.这很有可能,但涉及的编码比您可能准备做的要多得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.