简体   繁体   English

在Powershell脚本中运行.bat而不打开任何窗口

[英]running .bat in powershell script without opening any windows

I have written a PowerShell script that is going to interpret a mail's body for command's and create a .bat file to execute the commando's it found. 我编写了一个PowerShell脚本,该脚本将解释命令的邮件正文,并创建一个.bat文件以执行找到的突击队。 This script works, but the one big issues is that whenever is executes the .bat file, a command-prompt window flashes over the screen real quick. 该脚本可以工作,但是一个大问题是,每当执行.bat文件时,命令提示符窗口就会快速快速地闪烁在屏幕上。 I was wondering if it's possible to prevent this from happening? 我想知道是否有可能防止这种情况的发生?

My code: 我的代码:

$m.Body | Out-File cmd.bat -Encoding ascii -Append
.\cmd.bat | Out-File results.txt

Is there any command of property i have to set? 我必须设置任何属性命令吗? Thanks. 谢谢。

Answers and information can be found here . 答案和信息可以在这里找到。

From there, the selected answer, in case the link goes stale: 如果链接失效,从那里开始,选定的答案:

Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name. 将“ testing.bat”替换为批处理文件的名称后,将以下内容另存为wscript,例如hidecmd.vbs。

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false

The second parameter of oShell.Run is intWindowStyle value indicating the appearance of the program's window and zero value is for hidden window. oShell.Run的第二个参数是intWindowStyle值,该值指示程序窗口的外观,零值用于隐藏窗口。

The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx 参考在这里http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

I realize this question is more than 2 years old at the time I write this however there is still no official answer. 我在撰写本文时意识到这个问题已有2年多的历史了,但是仍然没有官方的答案。 Although I am very new to PowerShell, I think I have a more pure Powershell answer than using vbscript or COM. 尽管我对PowerShell非常陌生,但我认为与使用vbscript或COM相比,我对Powershell的回答更纯正。

Use Invoke Command: 使用调用命令:

Invoke-Command {cmd.exe /c cmd.bat} | Out-File results.txt

That Should do the trick. 这应该够了吧。 This will shell to cmd.exe and the /c will self terminate the shell on completion. 这将外壳到cmd.exe,并且/ c将在完成时自行终止外壳。 It will run within the current shell so no new window will open. 它将在当前shell中运行,因此不会打开新窗口。

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

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