How to automate user input to a executable? - powershell

I have a .exe file which when called from the Windows Command prompt, first connects to a server and then requests for user input as a confirmation.
Something like below:
C:\SomePath\AnotherFolder>TheApplication.exe download
Logging in to Vault server myserver.app.cosmos...
Logged in to the Vault server
Downloading will overwrite existing files.
Download scripts for context 'COSMOS' to folder:
C:\Work\MyFolder\TEST?
(y/n): n <--- n was my input
Press any key to exit... <--- here i pressed y
I want to automate this process.
I tried the below line from the Windows command prompt:
echo n|TheApplication.exe download
I even tried :
(echo n && echo y)|TheApplication.exe download <-- the y for the last user input request to close the app.
But in both cases its throwing the below error:
Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
at System.Console.ReadKey(Boolean intercept)
at System.Console.ReadKey()
at TheApplication.Program.Main(String[] args)
**disclaimer: TheApplication.exe is a third party executable. I cannot change anything inside it.
PS: If needed I can run this from a powershell console if its not possible from windows command prompt or I can also include the whole thing inside a batch script.
But any how I need to automate this.

If you can use PowerShell...
autoinput.ps1
Add-Type -AssemblyName System.Windows.Forms
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("ny")
The command is:
powershell .\autoinput.ps1 & .\TheApplication.exe

Related

Error running Metricbeat.exe commands in powershell

I am trying to install Metricbeat on a Windows 10 machine so we can start monitoring it. When I open Powershell and run the following commands:
PS > .\metricbeat.exe modules list
I get the error
I copied that command as is from the Metricbeat documentation. I have seen videos on youtube of people running similar commands successfully. Please, why am I getting that error and what can I do to get my metricbeat.exe powershell commands to work?
You're copying the command TOO literally.
you've entered in the prompt PS > .\metricbeat.exe modules list
where you should have entered .\metricbeat.exe modules list
the latter executes modules list action against an application named "metricbeat.exe" located in .\ which indicates the current directory.
the former executes a redirection > of the output of an application named PS or get-process with input of .\metricbeat.exe modules and an argument of list.
wherever you copied this command from intended "PS >" to represent the beginning of the prompt and you don't need to include it.
Just like the error says... :P

Run individual command as another windows user within batch file

I have the following batch file (in Jenkins). I would like to run only "SqlPackage.exe" command as another windows user. I have the domain user id and password for it.
Batch file:
CD Folder_Name
DEL Another_Folder
SqlPackage.exe /Action:Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:Server1 /op:Publish.sql
Copy Publish.sql Somewhere_Folder
I understand I can run the batch file as another user using windows runas feature. However, in this case, I would like to only run an individual command (sqlpackage.exe).
Is it possible?

how to redirect output of application in command window, which started with affinity?

the following command starts the C++ application in new command window with process affinity set to 0xF.
start /affinity F test.ext arg1 arg2
But the above command opens a new cmd window and closes immediately when test.exe ends. I tried the following to get output but it doesn't do anything.
start /affinity F test.ext arg1 arg2 ^> out.txt
I appreciate if you know how to do this on powershell.
Thanks
About Redirection
The PowerShell redirection operators are as follows, where n
represents the stream number. The Success stream ( 1 ) is the default
if no stream is specified.
Operator Description
Syntax
> Send specified stream to a file. n>
>> Append specified stream to a file. n>>
>&1 Redirects the specified stream to the Success stream. n>&1
# Examples
# Example 1: Redirect errors and output to a file
dir 'C:\', 'fakepath' 2>&1 > .\dir.log
This example runs dir on one item that will succeed, and one that will
error.
It uses 2>&1 to redirect the Error stream to the Success stream, and >
to send the resultant Success stream to a file called dir.log
# Example 2: Send all Success stream data to a file
.\script.ps1 > script.log
This command sends all Success stream data to a file called script.log
Also, a possible duplicate of the following
How to pipe all output of .exe execution in Powershell?
How to redirect the output of a PowerShell to a file during its execution
See also
Running external commands, can or will require special
consideration.
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Using Windows PowerShell to run old command line tools (and their
weirdest parameters)
Execution of external commands in PowerShell done right
Part 1
Part 2
Part 3
Quoting specifics

Running a gui app with Powershell without displaying the gui

I want to run an app (it does not natively support command line mode) on Windows that require 5 fields of generic data from a user. However, I want to run this app without opening/displaying the gui (a la command line like). Is this something that can be done with Powershell. If so, can someone point me in the right direction. Thanks in advance
PowerShell does not change how an application is executed versus how it is when executed at the command line or a run dialog. If the application can accept input via arguments when run then any of these methods for executing the application will work.
If you are asking if powershell can read from the console host, the appropriate cmdlet would be read-host. So you could read from the user and then run the command with the arguments you desire.
$user = read-host "Username:"
& examplecommand.exe $user

The system cannot find the file specified- Task Scheduler

I have batch script file, which should encrypt a file with pgp. I have defined a task in task scheduler to do this, but I am keep receiving the error"The system cannot find the file specified".
Interestingly, when I run the same line of script in my powershell , the encrypted file is successfully generated.
I was wondering if anyone knows what can possibly be wrong here?
I tried to give the full path in my batch script , and also added the pass in start in part, when defining the action.
the batch scrip code is here::
rem #echo off
#set path=c:\test;%path%
#set d=%date:~-4,4%%date:~4,2%%date:~-7,2%
#set d=%d: =_%
#set t=%time:~0,2%%time:~3,2%%time:~6,2% #set t=%t: =0%
Rem Generate PGP encrypted file
#echo Starting PGP... >> c:\apps\ftpLogs\test.log
gpg2 --batch --yes -r testkey --output c:\test\foo\test_20150505.pgp --encrypt c:\test\foo\test_20150505.txt >> c:\apps\ftpLogs\test.log
and the script that I ran in my powershell, which works fine, is this line:
gpg2 --batch --yes -r testkey --output c:\test\foo\test_20150505.pgp --encrypt c:\test\foo\test_20150505.txt >> c:\apps\ftpLogs\test.log
Finally I was able to resolve the issue. The problem was with the user authority. The batch script was suppose to encrypt a file and then ftp the encrypted file to the vendor's ftp server.
Apparently in Windows Server 2012 , the ADMINISTRATORS have the permission to create a file (here the encrypted file) while this user does not have the permission to send it. and SYSTEM user has the permission to send but not to create. (Both of them had this authority back in Win Server 2012).
So what did I do at the end, was to make two different batch script tasks and schedule them with 10 mins time distance. The first one was running the above code with ADMINISTRATOR privileges and the second one was sending it out with SYSTEMS.
If your batch file doesn't work, but the PowerShell script does, just run the PowerShell script from your scheduled task instead.
From the Scheduled Task, instead of entering the path to your batch file specify PowerShell.exe (or more likely C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe). Then in the Arguments box specify your parameters. You probably want to hide the window, so we'll include that in the arguments, and for this you can probably skip loading a profile, so we'll add that too. Then just use the -Command parameter to specify your code that you have in the question.
So your Program to run box should show:
C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe
Then the box that says "Add Arguments (optional)" should have:
-NoProfile -WindowStyle Hidden -command "gpg2 --batch --yes -r testkey --output c:\test\foo\test_$((get-date).ToString(yyyyMMdd)).pgp --encrypt c:\test\foo\test_$((get-date).ToString(yyyyMMdd)).txt >> c:\apps\ftpLogs\test.log"
On a side note, should that be gpg2 or pgp2? I just copied your command, but it seemed odd to me that they would name it like that.