问题描述
我在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文件中。 error screenshot
我当前正在使用AHK 1.1.33.02 感谢任何建议。
解决方法
似乎您尝试使用continuation section,但做错了。
第一个(
应该再向下一行。
这是固定版本。
我还放弃了传统的AHK语法,并切换了现代的expression syntax:
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的经验。
而且我真的不想自己运行它来尝试一下。