简体   繁体   English

如何从 Powershell 运行批处理文件?

[英]How do you run batch files from Powershell?

Traditionally, I have been able to do something like:传统上,我能够执行以下操作:

Start-Process -FilePath "\\server\Install.bat" -Wait

However every time I run that, a command prompt will flash on the screen then disappear and the installer never kicks off.但是,每次我运行它时,屏幕上都会出现一个命令提示符 flash 然后消失,安装程序永远不会启动。 But if I navigate to the batch file location and double-click it to run it manually it runs just fine.但是,如果我导航到批处理文件位置并双击它以手动运行它,它运行得很好。 I swear I have done this before, but is Powershell unable to run batch files?我发誓我以前做过,但是 Powershell 无法运行批处理文件吗?

EDIT: Batch file code:编辑:批处理文件代码:

SentinelAgent_windows_v4_4_2_143.exe /SITE_TOKEN=(sitecode) /SILENT

sitecode obviously contains the proper site code for our facility.站点代码显然包含我们设施的正确站点代码。

EDIT 2: Additional info:编辑2:附加信息:

My Start-Process contains the FQDN path to the batch file.我的Start-Process包含批处理文件的 FQDN 路径。 I have also tried the & with the FQDN to the batch file as well as attempting the CLI command right inside Powershell.我还尝试了使用 FQDN 到批处理文件的&以及在 Powershell 中尝试 CLI 命令。 All yield the same results.都产生相同的结果。 I have also tried including start SentinelAgent_windows_v4_4_2_143.exe as well as start /wait SentinelAgent_windows_v4_4_2_143.exe into the batch file with the same results.我还尝试将start SentinelAgent_windows_v4_4_2_143.exestart /wait SentinelAgent_windows_v4_4_2_143.exe包含到批处理文件中,结果相同。

I can still run the batch file manually by double clicking it and it works great.我仍然可以通过双击手动运行批处理文件,效果很好。 But no matter what I try to do to call it from Powershell, it fails with "SentinelAgent_windows_v4_4_2_143.exe is not recognized as an internal or external command"但无论我尝试从 Powershell 调用它,它都会失败并显示“SentinelAgent_windows_v4_4_2_143.exe 未被识别为内部或外部命令”

EDIT 3 as it would take too many characters to respond in the comments:编辑 3,因为在评论中回复需要太多字符:

CMD to PowerShell, run an exe with a XML was linked as a possible troubleshooting step. CMD 到 PowerShell,运行带有 XML 的 exe,作为可能的故障排除步骤。 I have tried the following all including the FQDN:我尝试了以下所有方法,包括 FQDN:

\\server.domain.com\SentinelAgent_windows_v4_4_2_143.exe /SITE_TOKEN=xxxxx /SILENT

& \\server.domain.com\SentinelAgent_windows_v4_4_2_143.exe /SITE_TOKEN=xxxxx /SILENT

Start-Process -Wait -FilePath "\\server.domain.com\SentinelAgent_windows_v4_4_2_143.exe" -ArgumentList '/SITE_TOKEN=xxxxx', '/SILENT'

Start-Process -Wait "\\server.domain.com\SentinelAgent_windows_v4_4_2_143.exe" '/SITE_TOKEN=xxxxx', '/SILENT'

All of which point directly to the.exe file with the FQDN in the search and all yield the following: "SentinelAgent_windows_v4_4_2_143.exe is not recognized as an internal or external command"所有这些都在搜索中直接指向带有 FQDN 的 .exe 文件,并且都产生以下内容: “SentinelAgent_windows_v4_4_2_143.exe 未被识别为内部或外部命令”

I also tried renaming the file to SentinelAgent.exe but it just returns the same error with the updated name.我还尝试将文件重命名为SentinelAgent.exe ,但它只返回与更新名称相同的错误。

I have also tried copying the entire directory down to C:\Temp and running the.bat/.exe command there - same error message.我还尝试将整个目录复制到 C:\Temp 并在那里运行 .bat/.exe 命令 - 相同的错误消息。

I have used:我用过:

Invoke-Expression -Command 'cmd.exe /C <executable path and args>'

This is specific to running command line commands.这是特定于运行命令行命令的。 An executable should not be an issue since the OS knows what to do with that type of file.可执行文件应该不是问题,因为操作系统知道如何处理该类型的文件。 As others have pointed out a.cmd or.bat file in this case provides no value and will create obscurity.正如其他人指出的那样,在这种情况下,a.cmd 或 .bat 文件没有任何价值,并且会造成晦涩难懂。

In this case the statement would be:在这种情况下,语句将是:

Invoke-Expression -Command 'cmd.exe /C \\server\SentinelAgent_windows_v4_4_2_143.exe /SITE_TOKEN=xxxxx /SILENT'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM