sc.exe is not returning output - windows

I have created a PowerShell script for changing password for Windows services.
Using sc.exe in code for the same.
When I execute the script it changed password in few servers but failed for others.
Next, I executed the command directly on the servers to check. The command is not returning anything.
On servers where it was successful, it will return a success message or some error if the password in not changed or if command not proper.
Also, same command is used with same values in all servers. Following is the command:
sc.exe config "$Service" obj= "$UserName" password= "$Password"

Related

Plink - Remote interactive connection with bash script does not seem to work properly

I have a redis database on a remote docker host, and I'd like to access it through a single ssh script command via plink.
The script is simple (redis-script.sh):
#!/bin/bash
echo "Enter Redis Password."
read -s pass
docker exec -it my-redis-container redis-cli -a $pass
Which works fine when I do a standard ssh connection via putty then run the script after login. I am able to enter the password and connect to the db:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379>
The problem is when I use plink, my plink command line:
plink.exe -t container-host /containers/redis-script.sh
I get this:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[6n
One issue is the mangled characters, but the biggest issue is that I can no longer type in any commands at this point. I am able to interact when it asks for the password, but once it gets into redis-cli, I cannot type anything.
Perhaps it's the docker exec command which is messing up the interactivity?
Any help is appreciated.

SSH to remote Windows machine using username and password through shell script

I am trying to SSH to a remote Windows machine (an AWS ec2 instance) by using an username and password of the remote machine. I need to automate this connection to run some remote commands from my script (either shell or Power shell) without prompting me for a password, My script shouldn't fail by expecting a password to be prompted
I don't want to use sshpass or any generated keys (by using ssh-keygen). Since the source machine where I run this command/script is not a dedicated machine, I may run it on a different machine everytime. I also gave a try to connect using the .PEM file provided by AWS as below (thought it could be easy while using it the script).
$ssh -i aws_keypair.pem Administrator#10.10.10.10
Administrator#10.10.10.10's password:
It is still expecting me for a password even if I used the .PEM file, I also tried to created an file 'authorized_keys' in the remote Windows machine under the path "C:\Users\Administrator.ssh\". Still it is prompting me for a password.
Expectation :
Connect to remote Windows machine using PEM file and run some remote commands.
(or)
It shouldn't prompt me for a password while I try for the connection from some script (shell/power shell).
Can be done without any 3rd party tools like this:
$env:TMPPW=Get-Content -Path 'secure_file.txt' ; $un='MyUserName'
$j=Start-Job -ScriptBlock{Start-Sleep -Seconds 1
(New-Object -ComObject wscript.shell).SendKeys("$env:TMPPW{ENTER}")}
& ssh.exe -q -4 -l $un 127.0.0.1 'whoami'
$env:TMPPW=([guid]::NewGuid()).Guid ; $env:TMPPW=$null
I am able to achieve this using Plink command (Installation of Putty is required on source machine to execute this command). So now, I am able to successfully pass the username and password with in the script (Shell script) and everything is working as expected.
FYI, I am pasting the exact command which worked for me
$echo y | plink -ssh Administrator#10.10.10.10 -pw abc123 "dir"

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.

Using psexec on a bat file that contains psservice

I have a bat file that tries to stop a service on a remote machine, the file contains the following
psservice \\remoteServerName -u domainName\userName -p password stop serviceName
where userName is a name of a user with Admin privileges on the remote machine.
If I run the last line using cmd then the requested service does stop.
I, however, run the bat file using psexec, since it contains more operation than just stopping the service. This is where my problem occurs:
If I run
psexec -u domainName\userName -p passsword batFilePath
the cmd window seems to get stuck.
But if I run
psexec batFilePath
then the psservice executes correctly.
My problem is that I need the user "domainName\userName" to be able to run the other commands in the bat file.
What can I do ? Why does the cmd got stuck when I gave psexec the parameters of the userName and password ?

shutdown redis-server from command line

I am trying to shutdown my redis-server from a redis-cli. Whenever I issue a command from a client I receive the error "(error) NOAUTH Authentication required." I have tried the commands "SHUTDOWN" and "SHUTDOWN NOSAVE".
I have also tried "redis-server stop" from another terminal window but received the error "# Fatal error, can't open config file 'stop'"
How can I shut down this server?
(I am on OSX).
Your Redis server is configured with a password apparently. Therefore, when using redis-cli, you'll need to issue the AUTH password command before any other command or else you'll be getting that error message (replace password with your server's password).
Alternatively, you can invoke redis-cli with the -a switch followed by your password for the same result.
To find the password of your server, open the Redis configuration file (by default /etc/redis/6379.conf) and look for the line starting with requirepass - whatever value is next to it, is the password.
1. redis-cli
2. auth yourpassword
3. shutdown
4. sudo service redis_6379 start
You have to manually edit the start/stop-service script:
sudo vi /etc/init.d/redis_6379
Find the following line:
$CLIEXEC -p $REDISPORT shutdown
And replace it with the following 'changeit' is where your password goes:
$CLIEXEC -p $REDISPORT -a changeit shutdown
Now you should be able to start and stop the service without any problems.
On Amazon EC2 instance I could restart local Redis like this:
sudo /etc/init.d/redis restart
P.S. If you are using Redis Authentication you have to pass -a <pass> parameter.

Resources