通过自动热键传递PowerShell变量

通过自动热键传递PowerShell变量,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 /rediscove

我在自动热键脚本块中遇到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 感谢您的建议。

您似乎试图使用a,但却错了。
第一个
应该再下一行

这是固定版本。
我还抛弃了传统的AHK语法,转而使用现代语法:

但是,我不能保证PowerShell命令的有效性,也不能保证您在那里传递它的方式。我没有真正的PowerShell经验。

我真的不想自己运行它来尝试。

手动重新键入引号来替换引号。不是引号,而是
-eq
周围的空格。它们可能是从网站复制的不间断空格或其他特殊字符。非常感谢!
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 "}"