我正在.bat文件中尝试这些命令
@echo off & setLocal EnableDelayedExpansion
powershell -NoLogo -NoProfile -Command ^
"$h = [int](Get-Date -Format "HH");" ^
"$diff = (7-$h)*3600;" ^
"if ($h -le 7 -or $h -ge 0) { ECHO $h; ECHO $diff; }"
但这会引发错误,表明命令无法识别。在这里,我尝试获取小时数并从7中减去$ h。然后将结果乘以3600,然后在控制台上打印出来。
谁能告诉我我在做什么错?
最佳答案
脱字号^
必须是连续行中的最后一个字符。在代码中,第一行中有一些尾随空格。
考虑具有空格的代码,通过在行的开头和结尾添加管道字符来说明。
|powershell -NoLogo -NoProfile -Command ^ |
| "$h = [int](Get-Date -Format "HH");" ^|
| "$diff = (7-$h)*3600;" ^|
| "if ($h -le 7 -or $h -ge 0) { ECHO $h; ECHO $diff; }"|
运行此命令将提供以下输出:
C:\Temp>t.cmd
Cannot process the command because of a missing parameter. A command must follow -Command.
PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
...
'"$h = [int](Get-Date -Format "HH");"' is not recognized as an internal or external command, operable program or batch file.
尽管删除尾随空格的方式是这样的,
| powershell -NoLogo -NoProfile-命令^ |
...
C:\Temp>t.bat
10
-10800
关于powershell - 从批处理文件运行Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60109526/