0

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. 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. 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.

2 Answers 2

2

I realize this question is more than 2 years old at the time I write this however there is still no official answer. Although I am very new to PowerShell, I think I have a more pure Powershell answer than using vbscript or COM.

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. It will run within the current shell so no new window will open.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.

The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

3 Comments

I'm using PowerShell, not vba. Don't think syntax is applicable?
@TomKustermans Perhaps not the syntax, but I would think it possible to do essentially the same thing in PowerShell - create a windows script interpreter and execute its Run command. Oh what the heck, let me Google it for you: computerperformance.co.uk/powershell/powershell_com.htm
It worked: PowerShell syntax: $WSH = New-Object -Com WScript.Shell and run .bat with : $WSH.Run("cmd.bat",0) | Out-File results.txt 0 parameter indicating it's hidden window indeed ;) thanks !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.