【问题标题】:Pass PowerShell Variables through AutoHotKey通过 AutoHotKey 传递 PowerShell 变量
【发布时间】:2020-12-24 06:19:09
【问题描述】:

我在 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 感谢任何建议。

【问题讨论】:

  • 通过手动重新输入来替换引号。
  • 不是引号,-eq 周围的空格。它们可能是从网站复制而来的不间断空格或其他特殊字符。

标签: powershell autohotkey


【解决方案1】:

您似乎尝试使用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 经验。
而且我真的不想自己运行它来尝试一下。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-05-13
  • 1970-01-01
  • 2021-03-01
  • 2013-05-01
  • 2014-07-10
  • 2018-10-11
  • 2011-10-19
  • 2014-12-25
  • 2015-06-23
相关资源
最近更新 更多