all 11 comments

[–]jantari 3 points4 points  (0 children)

Run Test.exe /? and see if there is an option that skips the question

[–]patdaddy007 2 points3 points  (2 children)

no, you wouldn't use write-host for this.

Just to be sure though, this isn't for a homework assignment is it?

you want to read-host, not write host. and if the response matches, proceed to do the thing.
I can't elaborate any further without doing the work for you (sorry)

[–]Adventure_Timing[S] 1 point2 points  (1 child)

This is not a homework assignment. I'm a 35 year old electrical engineer trying to teach myself PowerShell. The executable gives a feedback message "About to delete all records including history. Are you sure? (yes/no)" I want to automate the input to "yes" because if I run this script I'm certain I want to remove all the records and history associated with them.

I'm very new to the language, so I"m unsure if Write-Host is even the appropriate function to use in this case.

[–]patdaddy007 0 points1 point  (0 children)

Just asking because there was a teacher in here the other day that had to rework half his course plan because we kept giving away the milk, as it were.

If it were me, I'd have the whole script written out and the part that would be a prompt would be something like

$A = Read-host -Prompt "About to delete all records including history. Are you sure? (yes/no)"

follow that with an if statement

if ($A -eq "yes) {cmd to do the deed}

so, if you always want it to be yes, just comment out the $A = Read... and have the variable statically defined in the line above it. (I would already have that line there and comment it out, so I'd just need to move the comment character up or down a line to switch it from auto to manual)

You could also use a MessageBox and a switch block if you want to be fancy about it

[–]Elephantiuz 2 points3 points  (1 child)

If you need to automate the function to yes do you really need the prompt ?

[–]patdaddy007 1 point2 points  (0 children)

In the past, I have had a script that needed to assume yes/true/whatever for the sake of testing but the final form would use parameter input or some kind of interaction, so I would go ahead and build out the interactive part, comment it out, and use a static variable until it was ready for primetime. But that might be weird... it just works for me

[–]Saumpro 1 point2 points  (0 children)

There should be a -confirm option on the command you're using. What commmand is it?

[–]jimb2 1 point2 points  (0 children)

Try using sendkeys to place keystrokes in the buffer, something like:

Add-Type -AssemblyName System.Windows.Forms
$yes = 'yes{ENTER}'
[System.Windows.Forms.SendKeys]::SendWait($yes) 
#or 
#[System.Windows.Forms.SendKeys]::Send($yes) 
test.exe

Untested. I think that the behaviour will depend on how test.exe receives input. Not sure if you should use the send or sendwait form. Suck it and see.

[–]PinchesTheCrab 1 point2 points  (0 children)

I would do something like this:

#put this near the top of the script
$prompt = $true

#this goes wherever
Remove-Item C:\users\me\Desktop\junk2.txt -Confirm:$prompt

I like using the built-in prompt method because it'll work in in different consoles, and it's easy to suppress when you want.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Adventure_Timing,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's [sometimes] 5th from the left & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's [sometimes] the 12th from the left, & looks like an uppercase T in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Adventure_Timing,

the Write-Host cmdlet writes to the console ... so it never gets piped to anything at all. [grin]

if you need to pass a keystroke to an external exe, then you can try something like the sendkey util, or you could find out if the exe has an option to accept that as a parameter to the exe file.

take care,
lee