首页 > 解决方案 > 通过 AutoHotKey 传递 PowerShell 变量

问题描述

我在 autohotkey 脚本块中遇到 powershell 问题。我的 ahk 文件中的代码块如下:

psSetTime = (
    $service = Get-Service W32Time
    if ($service.Status -eq 'Stopped'){
    $service | Set-Service -StartupType Automatic
    $service | Start-Service -PassThru}
    w32tm /resync /rediscover
    w32tm /query /status
)
Run Powershell.exe -NoExit -command &{%psSetTime%}

加载文件时会弹出由此产生的错误。它指出“以下变量名包含非法字符”。错误屏幕中的字符绝对不存在于我的 ahk 文件中。 错误截图

我目前正在使用 AHK 1.1.33.02 感谢任何建议。

标签: powershellautohotkey

解决方案


似乎您尝试使用延续部分,但做错了。
第一个(应该再下一行。

所以这里是固定版本。
我还抛弃了传统的 AHK 语法并切换了现代表达式语法

psSetTime := "
(
$service = Get-Service W32Time
if ($service.Status -eq 'Stopped'){
$service | Set-Service -StartupType Automatic
$service | Start-Service -PassThru}
w32tm /resync /rediscover
w32tm /query /status
)"

Run, % "Powershell.exe -NoExit -command &{" psSetTime "}"

但是,我不能说 PowerShell 命令的有效性,或者你在那里传递它的方式。我真的没有 PowerShell 经验。
而且我真的不想自己运行它来尝试它。


推荐阅读