Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ctrl-C out of a Continuous Ping in Powershell 1

Status
Not open for further replies.

Brycspain

IS-IT--Management
Mar 9, 2006
150
US
Probably the wrong forum for this but I was wondering if anyone else has had this problem too. Or this by design?
 
Hey Bryc,

Not following your question. Are you asking if it is necessary to use Ctrl+C to stop a continuous ping? If so the answer is yes, that is by design.

Per the help file:
-t Ping the specified host until stopped.
To see statistics and continue - type Control-Break;
To stop - type Control-C.




I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
It's funny how you think you are being clear and when you look back at your question, it wasn't clear at all.

I'm setting up some continuous ping scripts within Powershell and as I try to stop the pings I'm not able to use any of the shortcuts I've used in the past i.e., Ctrl-C (main one), Ctrl-D, Ctrl-M, etc. My Powershell version is 1.0.0.0.
 
OK, makes more sense now.

Code:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Ping")
$wscript-new-object -com wscript.shell
$wscript.sendkeys("^C+{ENTER}")

PowewrShell doesn't have the appActivate method like VBScript does, so in the first line of code is loading the .NET Framework class Microsoft.VisualBasic. In the second line, we then call Visual Basic’s AppActivate method, passing this method the title of the window that we want to bring to the front. In this case it will grab whatever window starts with the word Ping. In the third line we create the WSHShell com object and then in the fourth use sendkeys to push out the key combination we need.

If you know the exact title of the Window you can activate it like this:
Code:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Test.ps1 - Notepad")

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

Thanks for the code. What does that do to fix my problem when typing ping -t in a Powershell window and not being able to Ctrl-C to stop the pings? Is your code something I have to do in my powershell profile? I'm not sure what you are telling me.
 
Sorry Bryc, you did not specify how you were calling that command.

When I manually type into a PowerShell window Ping -t www.yahoo.com I immediately start getting the replies. When I hit Ctrl+C it stops the command.

Your issue may be related to UAC. Do you have that turned on or off?

The code I provided was for ending the command via a script file (because I thought you were starting the ping via script).

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

You're correct...sorry again for the confusion. I'm in the dark ages and still using Windows XP 32-bit if you can believe it so UAC isnt my issue.

I will incorporate the new code in my script.

Thank you very much for your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top