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 文件中。 错误截图
我目前正在使用 AHK 1.1.33.02 感谢任何建议。
解决方案
似乎您尝试使用延续部分,但做错了。
第一个(
应该再下一行。
所以这里是固定版本。
我还抛弃了传统的 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 经验。
而且我真的不想自己运行它来尝试它。
推荐阅读
- proxy - 使用来自 azure sdk 的 MQTT 协议的应用程序在公司代理后面不起作用
- android - java.lang.IllegalStateException:CameraX 尚未初始化
- javascript - 如果特定的 div 在视口中淡入音频,如果 div 不在视口中淡出
- r - 如何绘制对数后验的轨迹(直至成比例)?
- mysql - Mysql 5.7 重复键的声明处理程序中的问题
- azure - 如何正确使用 CosmosDb 处理大于 2 Mb 的文档
- mysql - 多赛事锦标赛积分榜(任意数量的参赛作品)
- sql-server - 尝试在数据库中循环并创建函数时出现 SQL 错误
- html - Bootsrap 4 在移动设备上重叠粘性侧边栏
- c - 如何计算最长路径问题的复杂性?