Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - SendKeys to cmd.exe

Reply
 
Old 03-22-2008   #1 (permalink)
jm


 
 

SendKeys to cmd.exe

I can pass text to calc or notepad, but the same is not true for cmd or
winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no go. Well it
does at least open a command window or winword but will pass NO text…

If you have a moment – Thanks.

FUGURE1
Shell = newobject –com WScript.Shell
[void] $shell.Run(“calc”)

Start-sleep 1
If ($shell.AppActivate(“Calculator”))
{
“Calculator activated…”
Start-sleep 1
$shell.SendKeys(“10{+}”)
Start-sleep 1
$shell.SendKeys(“15”)
Start-sleep 1
$shell.SendKeys(“~”)
Start-sleep 1
$shell.SendKeys(“%F4”)
}

FIGURE2
Shell = newobject –com WScript.Shell
[void] $shell.Run(“cmd”)

Start-sleep 1
If ($shell.AppActivate(“cmd”))
{
“cmd activated…”
Start-sleep 1
$shell.SendKeys(“delete c:\temp\jess\*.*”)
Start-sleep 1
$shell.SendKeys(“~”)
Start-sleep 1
$shell.SendKeys(“y”)
Start-sleep
$shell.SendKeys(“~”)
Start-sleep 1
$shell.SendKeys(“exit”)
}


My System SpecsSystem Spec
Old 03-22-2008   #2 (permalink)
Shay Levi


 
 

Re: SendKeys to cmd.exe


Hi

FIGURE2 doesn't work because the AppActivate methos expects the cmd window
title. On my machine it's
invoked with the path of system32 in the title. Updating the AppActivate
argument runs the script without a problem.
You can also unify some keystrokes:


$shell = new-object com wscript.shell
[void] $shell.Run("cmd")
Start-sleep 1

If ($shell.AppActivate("c:\windows\system32"))
{
Start-sleep 1
$shell.SendKeys("del c:\scripts\temp\*.*~")

Start-sleep 1
$shell.SendKeys("y~")

Start-sleep 1
$shell.SendKeys("exit~")
}


Keep in mind that this is not a secure method, it'll most likely fail most
of he time due to the delete operation time length and the sleep intervals.
Why don't you delete the files with remove-item:

dir c:\scripts\temp\*.* | remove-item -force


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I can pass text to calc or notepad, but the same is not true for cmd
> or winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no go.
> Well it does at least open a command window or winword but will pass
> NO text
>
> If you have a moment Thanks.
>
> FUGURE1
> Shell = newobject com WScript.Shell
> [void] $shell.Run(calc)
> Start-sleep 1
> If ($shell.AppActivate(Calculator))
> {
> Calculator activated
> Start-sleep 1
> $shell.SendKeys(10{+})
> Start-sleep 1
> $shell.SendKeys(15)
> Start-sleep 1
> $shell.SendKeys(~)
> Start-sleep 1
> $shell.SendKeys(%F4)
> }
> FIGURE2
> Shell = newobject com WScript.Shell
> [void] $shell.Run(cmd)
> Start-sleep 1
> If ($shell.AppActivate(cmd))
> {
> cmd activated
> Start-sleep 1
> $shell.SendKeys(delete c:\temp\jess\*.*)
> Start-sleep 1
> $shell.SendKeys(~)
> Start-sleep 1
> $shell.SendKeys(y)
> Start-sleep
> $shell.SendKeys(~)
> Start-sleep 1
> $shell.SendKeys(exit)
> }

My System SpecsSystem Spec
Old 03-22-2008   #3 (permalink)
RichS


 
 

RE: SendKeys to cmd.exe

If you want to send key strokes through PowerShell have a look at Windows
Automation for PowerShell at
http://www.codeplex.com/WASP/Release...ReleaseId=9323

it will probably do what you want with out dropping back to the VBScript
objects
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"jm" wrote:
Quote:

> I can pass text to calc or notepad, but the same is not true for cmd or
> winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no go. Well it
> does at least open a command window or winword but will pass NO text…
>
> If you have a moment – Thanks.
>
> FUGURE1
> Shell = newobject –com WScript.Shell
> [void] $shell.Run(“calc”)
>
> Start-sleep 1
> If ($shell.AppActivate(“Calculator”))
> {
> “Calculator activated…”
> Start-sleep 1
> $shell.SendKeys(“10{+}”)
> Start-sleep 1
> $shell.SendKeys(“15”)
> Start-sleep 1
> $shell.SendKeys(“~”)
> Start-sleep 1
> $shell.SendKeys(“%F4”)
> }
>
> FIGURE2
> Shell = newobject –com WScript.Shell
> [void] $shell.Run(“cmd”)
>
> Start-sleep 1
> If ($shell.AppActivate(“cmd”))
> {
> “cmd activated…”
> Start-sleep 1
> $shell.SendKeys(“delete c:\temp\jess\*.*”)
> Start-sleep 1
> $shell.SendKeys(“~”)
> Start-sleep 1
> $shell.SendKeys(“y”)
> Start-sleep
> $shell.SendKeys(“~”)
> Start-sleep 1
> $shell.SendKeys(“exit”)
> }
>
My System SpecsSystem Spec
Old 03-22-2008   #4 (permalink)
jm


 
 

Re: SendKeys to cmd.exe

Hi Shay,
Thanks for the reply.
First off, all I need to do now is sendkey a "y" for yes and "enter".
I am new to scripting so I may be doing something wrong but I cannot get
this to work even with your code. I am going to try it on a non-vista
machine just in case that's an issue. When I open cmd on my computer, it
says this exactly: Administrator: C:\Windows\System32\cmd.exe
So I modified my script exactly like your changing the above to:
If ($shell.AppActivate("c:\windows\system32"))
If ($shell.AppActivate("c:\windows\system32\"))
If ($shell.AppActivate("c:\windows\system32\cmd.exe"))
If ($shell.AppActivate("administrator c:\windows\system32\cmd.exe"))

But none worked. Here is my exact code with how I believe it should work.
$shell = new-object –com wscript.shell
[void] $shell.Run("cmd")
Start-sleep 2
If ($shell.AppActivate("C:\Windows\System32"))
{
Start-sleep 2
$shell.SendKeys("dir~")
Start-sleep 2
$shell.SendKeys("y~")
Start-sleep 2
$shell.SendKeys("exit~")
#$shell.SendKeys("%{F4}")
}

Thanks,


"Shay Levi" wrote:
Quote:

>
> Hi
>
> FIGURE2 doesn't work because the AppActivate methos expects the cmd window
> title. On my machine it's
> invoked with the path of system32 in the title. Updating the AppActivate
> argument runs the script without a problem.
> You can also unify some keystrokes:
>
>
> $shell = new-object –com wscript.shell
> [void] $shell.Run("cmd")
> Start-sleep 1
>
> If ($shell.AppActivate("c:\windows\system32"))
> {
> Start-sleep 1
> $shell.SendKeys("del c:\scripts\temp\*.*~")
>
> Start-sleep 1
> $shell.SendKeys("y~")
>
> Start-sleep 1
> $shell.SendKeys("exit~")
> }
>
>
> Keep in mind that this is not a secure method, it'll most likely fail most
> of he time due to the delete operation time length and the sleep intervals.
> Why don't you delete the files with remove-item:
>
> dir c:\scripts\temp\*.* | remove-item -force
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > I can pass text to calc or notepad, but the same is not true for cmd
> > or winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no go.
> > Well it does at least open a command window or winword but will pass
> > NO text…
> >
> > If you have a moment – Thanks.
> >
> > FUGURE1
> > Shell = newobject –com WScript.Shell
> > [void] $shell.Run(“calc”)
> > Start-sleep 1
> > If ($shell.AppActivate(“Calculator”))
> > {
> > “Calculator activated…”
> > Start-sleep 1
> > $shell.SendKeys(“10{+}”)
> > Start-sleep 1
> > $shell.SendKeys(“15”)
> > Start-sleep 1
> > $shell.SendKeys(“~”)
> > Start-sleep 1
> > $shell.SendKeys(“%F4”)
> > }
> > FIGURE2
> > Shell = newobject –com WScript.Shell
> > [void] $shell.Run(“cmd”)
> > Start-sleep 1
> > If ($shell.AppActivate(“cmd”))
> > {
> > “cmd activated…”
> > Start-sleep 1
> > $shell.SendKeys(“delete c:\temp\jess\*.*”)
> > Start-sleep 1
> > $shell.SendKeys(“~”)
> > Start-sleep 1
> > $shell.SendKeys(“y”)
> > Start-sleep
> > $shell.SendKeys(“~”)
> > Start-sleep 1
> > $shell.SendKeys(“exit”)
> > }
>
>
>
My System SpecsSystem Spec
Old 03-22-2008   #5 (permalink)
CodeTestDummy


 
 

Re: SendKeys to cmd.exe

Try setting the name of the command prompt. Set the name to something
Unique. Try this:

$AppName = "My Unique Command Prompt Name"
$Shell = new-object -comobject WScript.Shell
[void] $shell.Run("cmd /k title " + $AppName, 1, $False)

$AppReady = $false

# Loop until you find the your app
while($AppReady -eq $False)
{
Start-Sleep 1
$AppReady = $shell.AppActivate($AppName)
}

If ($shell.AppActivate($AppName))
{
Write-Host "Found"
Start-sleep 1
$shell.SendKeys("ipconfig")
$shell.SendKeys("~")
$shell.SendKeys("exit")
Start-sleep 3
$shell.SendKeys("~")
}
else{Write-Host "App not found"}



"jm" <jm@xxxxxx> wrote in message
news:4B1D0469-9E25-490B-A7C9-138C31A37C74@xxxxxx
Quote:

>I can pass text to calc or notepad, but the same is not true for cmd or
> winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no go. Well
> it
> does at least open a command window or winword but will pass NO text.
>
> If you have a moment - Thanks.
>
> FUGURE1
> Shell = newobject -com WScript.Shell
> [void] $shell.Run("calc")
>
> Start-sleep 1
> If ($shell.AppActivate("Calculator"))
> {
> "Calculator activated."
> Start-sleep 1
> $shell.SendKeys("10{+}")
> Start-sleep 1
> $shell.SendKeys("15")
> Start-sleep 1
> $shell.SendKeys("~")
> Start-sleep 1
> $shell.SendKeys("%F4")
> }
>
> FIGURE2
> Shell = newobject -com WScript.Shell
> [void] $shell.Run("cmd")
>
> Start-sleep 1
> If ($shell.AppActivate("cmd"))
> {
> "cmd activated."
> Start-sleep 1
> $shell.SendKeys("delete c:\temp\jess\*.*")
> Start-sleep 1
> $shell.SendKeys("~")
> Start-sleep 1
> $shell.SendKeys("y")
> Start-sleep
> $shell.SendKeys("~")
> Start-sleep 1
> $shell.SendKeys("exit")
> }
>

My System SpecsSystem Spec
Old 03-23-2008   #6 (permalink)
Shay Levi


 
 

Re: SendKeys to cmd.exe

JM,

Automating CMD can be very tricky. If your command has a PowerShell or even
VBScript version then I would use it.
Nonetheless, try to be more specific, what is your end goal?


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi Shay,
> Thanks for the reply.
> First off, all I need to do now is sendkey a "y" for yes and "enter".
> I am new to scripting so I may be doing something wrong but I cannot
> get
> this to work even with your code. I am going to try it on a non-vista
> machine just in case that's an issue. When I open cmd on my computer,
> it
> says this exactly: Administrator: C:\Windows\System32\cmd.exe
> So I modified my script exactly like your changing the above to:
> If ($shell.AppActivate("c:\windows\system32"))
> If ($shell.AppActivate("c:\windows\system32\"))
> If ($shell.AppActivate("c:\windows\system32\cmd.exe"))
> If ($shell.AppActivate("administrator c:\windows\system32\cmd.exe"))
> But none worked. Here is my exact code with how I believe it should
> work.
> $shell = new-object com wscript.shell
> [void] $shell.Run("cmd")
> Start-sleep 2
> If ($shell.AppActivate("C:\Windows\System32"))
> {
> Start-sleep 2
> $shell.SendKeys("dir~")
> Start-sleep 2
> $shell.SendKeys("y~")
> Start-sleep 2
> $shell.SendKeys("exit~")
> #$shell.SendKeys("%{F4}")
> }
> Thanks,
>
> "Shay Levi" wrote:
>
Quote:

>> Hi
>>
>> FIGURE2 doesn't work because the AppActivate methos expects the cmd
>> window
>> title. On my machine it's
>> invoked with the path of system32 in the title. Updating the
>> AppActivate
>> argument runs the script without a problem.
>> You can also unify some keystrokes:
>> $shell = new-object com wscript.shell
>> [void] $shell.Run("cmd")
>> Start-sleep 1
>> If ($shell.AppActivate("c:\windows\system32"))
>> {
>> Start-sleep 1
>> $shell.SendKeys("del c:\scripts\temp\*.*~")
>> Start-sleep 1
>> $shell.SendKeys("y~")
>> Start-sleep 1
>> $shell.SendKeys("exit~")
>> }
>> Keep in mind that this is not a secure method, it'll most likely fail
>> most of he time due to the delete operation time length and the sleep
>> intervals. Why don't you delete the files with remove-item:
>>
>> dir c:\scripts\temp\*.* | remove-item -force
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
Quote:

>>> I can pass text to calc or notepad, but the same is not true for cmd
>>> or winword i.e. FIGURE1(calc) works perfect. FIGURE2(cmd) is a no
>>> go. Well it does at least open a command window or winword but will
>>> pass NO text
>>>
>>> If you have a moment Thanks.
>>>
>>> FUGURE1
>>> Shell = newobject com WScript.Shell
>>> [void] $shell.Run(calc)
>>> Start-sleep 1
>>> If ($shell.AppActivate(Calculator))
>>> {
>>> Calculator activated
>>> Start-sleep 1
>>> $shell.SendKeys(10{+})
>>> Start-sleep 1
>>> $shell.SendKeys(15)
>>> Start-sleep 1
>>> $shell.SendKeys(~)
>>> Start-sleep 1
>>> $shell.SendKeys(%F4)
>>> }
>>> FIGURE2
>>> Shell = newobject com WScript.Shell
>>> [void] $shell.Run(cmd)
>>> Start-sleep 1
>>> If ($shell.AppActivate(cmd))
>>> {
>>> cmd activated
>>> Start-sleep 1
>>> $shell.SendKeys(delete c:\temp\jess\*.*)
>>> Start-sleep 1
>>> $shell.SendKeys(~)
>>> Start-sleep 1
>>> $shell.SendKeys(y)
>>> Start-sleep
>>> $shell.SendKeys(~)
>>> Start-sleep 1
>>> $shell.SendKeys(exit)
>>> }

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Strange Sendkeys() problem PowerShell
Using sendkeys in a web form VB Script
Sending Alt-Space with SendKeys in C# .NET General
sendkeys, ie7, forms, any new bugs? VB Script
Sendkeys problem with Vista Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46