微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过AutoHotKey传递PowerShell变量

如何解决通过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文件中。 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的经验。
而且我真的不想自己运行它来尝试一下。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。