How to set environment variable and execute command in one line (PowerShell)? - bash

How can I set value of environment variable and execute command in one line in PowerShell?
I have this:
PGPASSWORD=db_pass psql -U db_user -d db_name -a -h localhost -f some.sql
and it's working great in Linux.
How can I translate above to PowerShell command which will work on Windows Server 2012 - Windows 10?
I tried:
SET "PGPASSWORD=db_pass" & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql
and I get error: "The & is reserved for future use..." - Windows 2012.

Try this:
$env:PGPASSWORD='db_pass'; & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql
In reality this is two commands separated by a semi-colon so they can be run as a single line.
Note also that from PowerShell you set environment variables via $env:<name of var>.

just add a trailing command to the compound list:
;$env:PGPASSWORD=''
to get
"C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql; $env:PGPASSWORD=''
you might have to pipe add a sleep in there between the command and last reset of the variable, if the command is launched in asynchronous fashion otherwise you might end up resetting the variable before its use by the launched program. Mileage may vary.
It's a hack, but it works better than leaving the password in the environment forever.

Related

How to use PGPASS file in Powershell to avoid password prompt?

I had to automate my postgre database backup. As instructed by my software vendor I am trying to use pg_dump.exe (see below) file to take a backup but that prompts me for password.
.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo
So googled and found that as per "https://www.postgresql.org/docs/9.6/libpq-pgpass.html" I can create a pgpass.conf file within 'C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf" which I did.
Then I tried to pass data of pgpass.conf file to env variable before executing my pg_dump command. But it is not working. Still I am getting prompt to enter password. This is the content of pgpass.conf file: *:*:*:postgres:password
Below is the code I am trying in PowerShell,
$Env:PGPASSFILE="C:\Users\User1\AppData\Roaming\postgresql\pgpass.conf"
cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL\9.6\bin"
.\pg_dump.exe -h localhost -p 4432 -U postgres -v -b -F t -f "C:\Backup\Backup.tar" Repo
Why am I still being asked for password?
When I type following code $Env:AppData I get following response "C:\Users\User1\AppData\Roaming"
Everywhere there are guidance on how to use it in UNIX or command prompt but not in powershell. Any help is appreciated. Also if you could direct me how to secure this password file then it will be great.
With password prompt I cannot automate it with windows task scheduler.
I suspect you have a suitable solution, however, as a quick (and not secure) workaround via the command prompt, you can use the variable PGPASSWORD to hold the password then run the backup script.
A sample might be something like:
SET PGPASSWORD=password
cd "C:\Program Files\Qlik\Sense\Repository\PostgreSQL\9.6\bin" pg_dump.exe -h localhost -p 4432 -U postgres -b -F t -f "d:\qs_backup\QSR_backup.tar" QSR
Rod
I have yet to get the damned thing to work yet, but I did find this:
-w
--no-password Never issue a password prompt. If the server requires password authentication and a password is not available by other means
such as a .pgpass file, the connection attempt will fail. This option
can be useful in batch jobs and scripts where no user is present to
enter a password.
I don't see a -w parameter in your call to pg_dump
I used pg_hba file to allow connection "trust" this is riskier method but I had to get things done ASAP. Thank you for your time and effort

Running psql from shell always ask for connection details

I am trying to run psql command from windows command line. However, command always ask for connection details even though connection details are given.
I have tried below two commands but none works:
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat -f d:\test.sql postgresql://postgres:password#localhost:5432/testdb
and
"C:\Program Files\PostgreSQL\12\scripts\runpsql.bat" -h localhost -d testdb -U postgres -p 5432 -f d:\test.sql
I have created password file to store password as mentioned here.
However, in command line, it asks for host name, database and other details.

Best way to set environment variable and execute command in one line

I want to make a backup with PostGreSQL pg_dump command with command line like :
"<c:\Program Files\PostgreSQL\9.6\bin\pg_dump>" -h localhost -p 5432 -d test_backup_bat -U user -f D:\destination_backup\test.backup
but I need to set PGPASSWORD before as environment variable to execute the backup command
PGPASSWORD=mypassword
How can I make this in only one command line into Windows CLI ?
Setting a variable and using it inside the same line in a batch file or a command line can be "tricky" as the full line/command is parsed before the variable is set.
BUT, in your case, you don't need to do anything special because you don't need to use the variable in the command except for setting it. The pg_dump just reads the contents of the environment variable from its own environment block that is inherited from the parent cmd process. If the parent process has the variable defined, then pg_dump can read it from its own copy.
So, you just need two commands (one to set the variable and one to start the new process) inside the same line, executed one after the other. This is done with the & operator:
set "PGPASSWORD=myPassword" & "c:\Program Files\PostgreSQL\9.6\bin\pg_dump" -h localhost -p 5432 -d test_backup_bat -U user -f D:\destination_backup\test.backup
To Set:
SET PGPASSWORD=mypasssword
To Verify:
echo %PGPASSWORD%

SSH from Local to A, A to B and run multiple commands on B

Im currently using the line of script below to ssh from my local machine to a server (lets call it ip-address1) then from that machine i want to ssh to another machine (lets call this machine ip-address2). The script i use is as follows:
sshpass -p mypassword ssh -tt user#ip-address1 ssh -tt -i /root/.ssh/vm_private_key user#ip-address2 "pwd; ls;"
The problem is only the first command (pwd) executes on ip-address2 then it closes and the ls command executes on ip-address1 before it then closes. I want both commands to execute on ip-address2. The output in my terminal is something like the following:
/home/user (pwd command executing here)
Connection to ip-address2 closed.
//files then get outputted here (ls command executes after ip-address2 has
closed)
Connection to ip-address1 closed.
I think there may be something wrong with my quotation but i cant figure out what. Please help.
Thanks.
I don't have any way to test this, but try the following:
sshpass -p mypassword ssh -tt user#ip-address1 \
"ssh -tt -i /root/.ssh/vm_private_key user#ip-address2 'pwd; ls;'"
You definitely need to quote the entire command you want to run on the ip_address1, including the command you'll pass to ip_address2.
Edit
I'm in an environment where I have multiple machines to test; the following command works for me:
ssh snewell#<host sanitized> \
"ssh <host2 sanitized> 'hostname; ls -a <path sanitized>;'"
hostname definitely displays the result of the final server (host2), and ls is listing a directory that the first host doesn't have.

NCFTPPUT command problem

I using passive mode FTP command provide by NCFTP, Currently i want to pass a raw ftp command after file transferred. i found that got an option to do that:
ncftpput -u user -p password -X "rename 123.exe 1234.exe" host /path C:\123.exe
however, it is not working. It can put the file, but rename command not working.
Have anyone did that before?Pls help
-X use RAW FTP commands
Use the following syntax:
ncftpput -u user -p password -X "RNFR 123.exe" -X "RNTO 1234.exe" host /path/123.exe
It works with ncftls as well. It is more immediate if you what you have to do is just a rename without actually uploading anything on the FTP server.
(-W is like -X. The only difference is that it does the rename immediately after logging in)
Here is the syntax:
ncftpls -u name -p psw -W "RNFR FTPfolder/anotherFolder/OLDname.txt" -W "RNTO FTPfolder/anotherFolder/NEWname.txt" ftp://ftp.name.org

Resources