【发布时间】:2019-10-25 07:09:55
【问题描述】:
我正在尝试通过 CMD shell 运行以下 PowerShell 代码:
$logfile = "x:\test.log"
try {
get-service
Add-Content - Path $logfile -Value "It Worked"
} catch {
Add-Content -Path $logfile -Value $_.Exception.Message
}
从一个 CMD 脚本我调用脚本如下:
Powershell.exe -executionpolicy bypass -command "I paste the code above
here"
我也试过如下:
Powershell.exe -executionpolicy bypass -command "& 'Command From Above'"
从错误中可以看出,它似乎并没有尝试运行整个命令,因为它似乎是在尝试运行我的日志命令:
如果我运行一些简单的东西,它可以毫无问题地工作。如下:
Powershell.exe -executionpolicy bypass -command "get-service"
【问题讨论】:
-
这是一个有趣的问题,但请务必发布您正在执行的实际命令,以及您收到的错误消息,而不是(仅)屏幕截图。
标签: powershell cmd