Jump to content
Software FX Community
Sign in to follow this  
gaurhoth

Hide cmd window for external command line app?

Recommended Posts

I have a powershell script that uses an external command line application to retreive data. However, when I pipe this to Out-Gauge with -refresh parameter, I get a CMD window that opens and closes each time the gadget refreshes. The powershell host hides any external cmd window... is there a configuration I'm missing for PowerGadgets to hide any external cmd windows? Here is a VERY simple example that shows the behavior:

Contents of PING.PS1:$output = ping www.google.com -n 1$output[ 6 ].split(" =")[ 6 ].replace("ms","")PS C:\ps\bin> .\ping.ps1 | % { out-gauge -value $_ -floating -refresh 0:0:5 }PS C:\ps\bin>

Share this post


Link to post
Share on other sites

Not sure how to stop [ 6 ] from being converted to a "devil smiley"... so replace any devils with [ 6 ] (remove spaces).

(Okay figured it out... added spaces in code example... but now I can't delete this message... what fun!)

Thanks.

Share this post


Link to post
Share on other sites

Unfortunately we are not aware of any way of hooking into how PowerShell runs "native" commands to avoid displaying the console window. The following is a mix of Lee Holmes post and Tim Scarfe post to try to generate a script that you can call when invoking native commands:

Contents of gpo.ps1 (get-processoutput)

param([string] $processname, [string] $arguments)

$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow=1
$psi.RedirectStandardOutput = 1
$psi.UseShellExecute=0
$psi.FileName = $processname
if ($arguments) {
 $psi.Arguments = $arguments
}

$process = [system.Diagnostics.Process]::Start($psi)
$process.WaitForExit()
$output = $process.StandardOutput.ReadToEnd()
$str = new-object System.String 13,1
foreach($line in $output.Split(10)) {
 $line.Replace($str,"")
}

The lines after ReadToEnd are my contribution and I am not terribly proud of them, but it was the closest I got to try to mimic the output you get when you run the commands natively. There is one difference though between running ping natively and using gpo that you can see from the following

PS C:\>$a = ping www.google.com -n 1
PS C:\>$a[0]

PS C:\>$a[1]

PS C:\>$a[2]
Pinging www.google.com [64.233.161.99] with 32 bytes of data:

PS C:\>$a = gpo ping "www.google.com -n 1"
PS C:\>$a[0]

PS C:\>$a[1]
Pinging www.google.com [64.233.161.99] with 32 bytes of data:

So even though when you run both commands the output looks identical, it seems as I am still not breaking the lines in the same way they do. If you ask me, I would say the empty line you get when running ping should be line0 and the "Pinging ..." line should be line1 so using gpo could make the line guessing game a little easier :-)

Now if you modify ping1.ps1 as follows

$output = gpo ping "www.google.com -n 1"
$output[3].split(" =")[ 6 ].replace("ms","")

You will get out-gauge to refresh properly without the flashing console window.

Regards,

JuanC

Share this post


Link to post
Share on other sites

We know it is a hack and we spoke to one of the PowerShell team members about this, what we need is for them to allow apps hosting PowerShell to have control over how processes are run. If they supported this, it would just be a matter of setting some properties such as CreateNoWindow

If you (and others) could request this through the connect site, it will increase the chance they would consider it for V2 :-)

 JuanC

Share this post


Link to post
Share on other sites

1. I can create something on the connect site if nobody else has access.  They seemed to have since 'hidden' PowerShell so it is hard to find it to create any items.

2. NetCmdlets (www.nsoftware.com/powershell) has a 'send-ping' cmdlet which may help out here.  It is giving an error in my install and I've contacted /n software on it.  It is currently in beta2...

Share this post


Link to post
Share on other sites

http://www.codeplex.com/PowerShellCX 

PSCX 1.1 was just recently released, and includes a cmdlet named ping-host:

NAME
  Ping-Host

SYNOPSIS
  Sends ICMP echo requests to nework hosts.


SYNTAX
  Ping-Host [-HostName] [[-Count]] [[-BufferSize]] [cut off here...]
  Parameters>]


DETAILED DESCRIPTION
  Sends ICMP echo requests to nework hosts.

Then you have access to several properties, and no annoying popup window.

Sample.zip

Share this post


Link to post
Share on other sites

Although I agree both NetCmdlets and PSCX are the right choice if you want to resolve the specific ping issue (you get better performance as the functionality you need is executed in-proc instead of launching a separate process + you can more easily pipe these cmdlets to other cmdlets), there is still a huge number of apps out there that will probably never have a cmdlet replacement so it is important for people to be aware of ways to invoke those apps without the flashing window.

JuanC

Share this post


Link to post
Share on other sites

Thanks for a great utility. When I try and use gpo.ps1 I get the following error:

The term 'gpo' is not recognized as a cmdlet, function, operable program, or script. 

How do I register this file so it is visible in my environment?

Thanks! 

Share this post


Link to post
Share on other sites

To be able to just run it as "gpo", you will need to put the .ps1 script in a directory that is in your path (or modify your path to include your script directory).

Otherwise, you will need to call it as ".\gpo" or ".\gpo.ps1".

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...