通过PowerShell运行.cmd文件

时间:2013-12-06 12:03:01

标签: windows powershell cmd

我正在尝试使用PowerShell在远程服务器上运行.cmd文件。

在我的.ps1脚本中,我试过这个:

C:\MyDirectory\MyCommand.cmd

导致此错误:

C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet,
function, script file, or operable program.

这个

Invoke-Command C:\MyDirectory\MyCommand.cmd

导致此错误:

Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.

我不需要将任何参数传递给PowerShell脚本。我正在寻找的正确语法是什么?

4 个答案:

答案 0 :(得分:17)

Invoke-Item将查找文件类型的默认处理程序并告诉它运行它。

它与在资源管理器中双击文件或使用start.exe基本相同。

答案 1 :(得分:9)

尝试调用cmd /c C:\MyDirectory\MyCommand.cmd - 这应该有效。

答案 2 :(得分:3)

转到C:\ MyDirectory并尝试:

.\MyCommand.cmd

答案 3 :(得分:0)

要将批处理文件运行或转换为PowerShell(特别是如果您希望使用证书对所有计划任务脚本进行签名),我只需创建一个PowerShell脚本,例如deletefolders.ps1。

在脚本中输入以下内容:

cmd.exe /c "rd /s /q C:\#TEMP\test1"

cmd.exe /c "rd /s /q C:\#TEMP\test2"

cmd.exe /c "rd /s /q C:\#TEMP\test3"

*需要将每个命令放在一个新行上,再次调用cmd.exe。

此脚本现在可以从PowerShell签名并运行,直接将命令输出到命令提示符/ cmd。

这比运行批处理文件更安全!