How to handle user interactive commands in Windows power shell? - windows

I need to execute the following command from Windows power shell:
PS C:\automation> ./test.exe get data "user\test" --outputfile test.csv
Please enter the Password:
Connecting database...
out file available in test.csv
PS C:\automation>
I need to enter the password, when its prompting for the password, in "Please enter the Password:". Then it will connect the database and gives output file.
Please guide me on how to automate this without using any 'subprocess' or 'pexpect' kind of things.

you can use wexpect which is a python module.Else use the one i created https://drive.google.com/file/d/0B-4scr3fQpEcRlBDSTNLU252UzA/view?usp=sharing
Sample powershell script is attached in that zip.
Read this as well:
https://github.com/anilchander1/WindowsConsoleAutomator/blob/master/README.md
Make sure that you have the latest Microsoft VC++ redistributable is installed.

Related

psql asks for password and does not read from pgpass.conf

I have installed my Postgresql database on a Windows server environment. I'd like to schedule a job using Windows Task scheduler to run every night so I need to run the following command without asking for password:
psql -U myUserName-d myDBName -c "select MyFunctionName()"
When I run the above query in my cmd shell, it asks me for password. When I enter the password manually, the function is correctly run.
So my solution is to read from the pgpass.conf file so no password is required.
Here are the things I have done to achieve this:
I created the pgpass.conf file in a directory I created in the %appdata% (AppData\Roaming\postgresql to be precise).
Here are the contents of this file:
localhost:5432:myDBName:myUserName:myPassword
I have also tried with the value 127.0.0.1 instead of localhost above.
I, then, added the an environment variable (in the user variables for administrator list) called PGPASSFILE and gave it the pgpass.conf location.
;C:\Users\administrator\AppData\Roaming\postgresql\pgpass.conf
Finally I stopped and restarted my Postgres service on Windows services and re-ran the command. But it is still asking for password.
How can I let my command know from where to read the password?
If you don't want to set the PGPASSFILE environment variable, put the password file in the standard location %APPDATA%\postgresql\pgpass.conf as described by the documentation.

Export Windows credentials with CMDKEY or similar batch equivalent

I am looking to export Windows credentials to another Windows machine. So far in Windows, all I have is the GUI option to backup / restore, but no options in CMDKEY to backup / restore all Windows credentials. Is there a command line equivalent to the following?
First of all, from a security stand point, having an inbuilt command line utility to export security credentials can lead to them being compromised. Someone who gains unauthorized access to your machine remotely to run shell commands or install an program that executes to dump your credentials and then send them somewhere else can do that. That said, I have not come across any inbuilt tools to do that. BUT, that doesn't mean you can't.
cmdkey is a tool that you can use to manage credentials from the command line.
There is a PowerShell tool by Microsoft called PowerShell Credentials Manager that shows all your credentials. You can then pipe that to an output file.
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde
Show all: CredMan.ps1 -ShowCred | Out-File **your-file**
Add New
.\CredMan.ps1 -AddCred -Target 'DemoTgt' -User 'DemoUser' -Pass 'DemoPass'
Remove .CredMan.ps1 -RemCred **cred name**
Read on some ways an attacker can compromise your system in blog post Dumping Windows Credentials.

Answer prompt on remote machine via script

Following situation:
From a Windows machine, I log into a remote Linux machine via
plink -v hostname -l username -pw password
Upon logging in, I am greeted with a 'Welcome to AIX Version 5.3!' and asked to choose a login case (at which prompt I need to press '2'), and then to choose a datapool (at which point I need to press '1').
However, I don't want to press anything. I want all of this to happen through a script. How can I automate answering the prompts?
Thank you in advance for any help.
I found a solution:
plink -v hostname -l username -pw password << input.txt
where input.txt is:
2
1
exit
Additionally, all commands which I would like to give to the remote terminal can be included in this input.txt file.

Running Unix scripts remotely from Windows terminal and passing back prompts

I'm using plink to run a script on a remote server (Linux) from a Windows machine. Part of the script prompts for inputs (authentication to various other remote servers that use different credentials). I don't want to store the password in the script as each use will be using their own for auditing reasons.
I need the prompt to be transmitted to the Windows terminal window and I need the input transmitted back to the Linux box. Additionally I need to write log all this into a file, like this:
plink username#unixbox /etc/scrips/myscript.bash > report.txt
At the moment the above works but all that prints to report.txt is the prompts
please enter password for reportBox1?
please enter password for reportBox2?
Instead I need it to send the password prompt and input to the Linux box to continue running the script as it normally would, only remotely. So the output of report.txt would read:
please enter password for reportBox1? *
File 1
File 2
File 3
please enter password for reportBox2? *
Data a
data b
data b
Hope that makes sense. If there's something better than plink can be used such as putty's ssh.exe please let me know that one instead.
First off: plink is PuTTY's ssh.exe.
If you want to be able to answer the password prompt on the Windows machine, you need to tell plink to allocate a pseudo-terminal with -t:
plink -t username#unixbox /etc/scrips/myscript.bash
Now you get the prompt and input will be sent back. But if you redirect STDOUT to report.txt...
plink -t username#unixbox /etc/scrips/myscript.bash > report.txt
...you won't see the prompt, because it's redirected into report.txt (although the script still runs and waits for your input). To get around this, you need some tool which allows you to redirect the output to multiple destinations - STDOUT and report.txt at the same time. In the *nix world, the command for this is tee. There are ports of tee for Windows:
as part of GnuWin32 (this is what I'm using)
as part of UnxUtils
as batch, perl and rexx versions
Having set one of those up, you'd do:
plink -t username#unixbox /etc/scrips/myscript.bash | tee report.txt
Security note: If the password prompts in the script on the Linux machine echo what was input, the passwords will of course also be logged in report.txt, which might be a problem.

how to log-in and execute ftp commands in native windows telnet client program using only one command

What I want to do is issuing one command that contains ftp commands that I want to execute. I want my command to contatin username, password and commands.
I only can use windows native ftp client program, and it seems it does not provide any help regarding this. What should I do?
did you read 'ftp /?' for example
Ftp -s:YouFileWithFtpCommands

Resources