0

I am trying to automate a backup script, but the cloud-backup provider that I am using has a poorly optimized CLI tool.
When starting for the first time, it is expected to run and then get user input from stdin.
example:

c:/backup> jotta.exe login
accept licence (y/n):

And then after that:

enter token:

The best solution would have been if the original developers had made these accessible as arguments/flags when running the program, but unfortunately that is not the case.

Is there any way I can pass "the next input" to the program or a queue of arguments to the program with PowerShell?

Bjamse
  • 333
  • 5
  • 17
  • PowerShell does not control executables, it will run them by calling cmd.exe and they take over. If the provider does allow passed input then it can't be done natively via PowerShell. Your only option is using Sendkeys to the cmd.exe window. Note: though sendkeys isa thing, it is not without its issues as well. Menaing, perfdormances-wise, it can /will be more slow ir faster. – postanote Feb 13 '21 at 03:46

2 Answers2

2

Continuing from my comment. So, something like this: It starts cmd.exe and a separate process, waits for 3 seconds, and sends the defined key methods to the active cmd.exe window. This could have easily been done via VBScript/WMI as well. So, not specific to PowerShell.

Example 1 - Calling cmd.exe directly responding to a prompt

Clear-Host
Add-Type -AssemblyName System.Windows.Forms
Start-Process -FilePath 'cmd.exe'
Start-Sleep -Seconds 3
[System.Windows.Forms.SendKeys]::SendWait('hello world')
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait({Date})
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}{ENTER}")

Example 2 - Calling a bat/cmd file which called cmd.exe and responding to a prompt

Clear-Host
Add-Type -AssemblyName System.Windows.Forms
'Running bat/cmd file and responding to it.'
Start-Process -FilePath 'C:\Scripts\TestSendKeys.cmd'
Start-Sleep -Seconds 3
[System.Windows.Forms.SendKeys]::SendWait('Y')
Start-Sleep -Seconds 3
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
'Done processing bat/cmd file.'

Also FYI...

about_Command_Precedence

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7

PowerShell: Running Executables https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

postanote
  • 15,138
  • 2
  • 14
  • 25
2

You can try to pipe to stdin of jotta.exe, line by line. This requires that the program actually reads from stdin as opposed to reading from the keyboard directly, in which case this answer applies.

'y', 'thetoken' | .\jotta.exe login

Here we are piping an array of strings to jotta.exe in the current directory (denoted by .\). Each element of the array is one line of input, which will be queued until the application reads from stdin.

You could also pass both lines as one string, embedding the linefeed character, e. g.:

"y`nthetoken" | .\jotta.exe login

Alternatively specify the full path of jotta.exe:

'y', 'thetoken' | & 'C:\Program Files\JottaCLI\Jotta.exe' login

(I've just made this path up, correct it as needed.)

The call operator & is required because we are using a quoted path. See this answer for details.

zett42
  • 25,437
  • 3
  • 35
  • 72