首页 > 解决方案 > 制作批处理文件时遇到问题

问题描述

我正在尝试制作一个 bacth 文件,该文件将制作第二个 bacth 文件并将其内容设置为:

电源外壳关机 /s /f /t 0

我尝试使用的命令是 powershell set-content "shutdown.bat" "powershell shutdown /s /f /t 0"

当我运行它时,我得到了这个:

Set-Content : A positional parameter cannot be found that accepts argument 'shutdown'.
At line:1 char:1
+ set-content -Path shutdown.bat -Value powershell shutdown /s /f /t 0
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand

标签: powershellbatch-file

解决方案


这个 ...

powershell shutdown /s /f /t 0

... 不是使用 Powershell 运行外部命令行工具或其他程序的方式。要运行外部工具,这是一个有据可查/谈论的事情......

PowerShell:运行可执行文件

解决 PowerShell 中的外部命令行问题

在 Powershell 中运行外部命令的 5 大技巧

使用 Windows PowerShell 运行旧的命令行工具(以及它们最奇怪的参数)

在 PowerShell 中正确执行外部命令

...以及相当日常的活动。

在做你想做的事情时,正确的引用和参数终止是一项要求。

所以,像这样的东西...

powershell -ArgumentList '-Command  &{ shutdown /s /f /t 0 }'

推荐阅读