Powershell command certUtil ssh and exitCode - powershell

I have a problem with a powershell command :
powershell -command "& { ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=2 server#server -x -p 22 CertUtil -hashfile program.jar MD5 }"
I've got this response :
CertUtil: -hashfile command FAILED: 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND)
CertUtil: The system cannot find the file specified.
That is the response I want. BUT this is not an error code. It is an output message. And the LastExitCode is '0'.
I need a true error code without analyzing the output message.
I tried to manipulate the command but without success. I don't understand why the generated error does not throw a real error in Powershell.

It seems that if I use this command (with the use of execution policy), I get a non '0' exit code. It may be a goot solution even if I'm not sure of the reason why. Many thanks for your help.
powershell -ExecutionPolicy Bypass -Command "& ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=2 server\server -x -p 22 CertUtil -hashfile program.jar MD5"; echo $LASTEXITCODE
CertUtil: -hashfile command FAILED: 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND)
CertUtil: The system cannot find the file specified.
1

Related

Error when try to send email with sendEmail

I want to use sendEmail to send automated emails.
If i run the following line separately it works:
sendemail.exe -f "payslip#dgroup.com" -t "dn.john#dgroup.com" -u "Payslip: DN JOHN, marca 1567, may 2021" -o message-file=mail_body_payslip.txt -o tls=no -o username="payslip#dgroup.com" -o password="hfsjfb&^%" -s webmail.dgroup.com:26 -o timeout=5 -xu payslip#dgroup.com -xp hfsjfb&^% -a "C:\Payslips\encrypted\2021_05_1702_DN_JOHN.pdf" -l email_log.txt
However if i try to run it from a bat file it doesn't work.
Here is the bat file:
#echo.
#echo.
echo Starting email session... >> email_log.txt
date /t >> email_log.txt
time /t >> email_log.txt
#echo.
#echo Verifying files...
#echo.
#echo Sending mails...
sendemail.exe -f "payslip#dgroup.com" -t "dn.john#dgroup.com" -u "Payslip: DN JOHN, marca 1567, may 2021" -o message-file=mail_body_payslip.txt -o tls=no -o username="payslip#dgroup.com" -o password="hfsjfb&^%" -s webmail.dgroup.com:26 -o timeout=5 -xu payslip#dgroup.com -xp hfsjfb&^% -a "C:\Payslips\encrypted\2021_05_1702_DN_JOHN.pdf" -l email_log.txt
#echo.
#echo.
#echo Payslip was sent. Verify email_log.txt
#echo.
#pause
When i run the bat file I get the following error:
Jun 16 14:01:55 srv3 sendemail.exe[2532]: ERROR => Connection attempt to localhost:25 failed: IO::Socket::INET: connect: Unknown error
If I run telnet localhost:25 I get this:
Could not open connection to the host, on port 23: Connect failed
What could be the problem? Why is running fine separately but executed from bat file I get error?
I've found the problem.
When you run the bat file the "%" character found in the password was replaced with "26" therefore the password was incorrect.
The solution is to use %% instead of %.
Two %% equals to one %.

Powershell - Run SCP command using CYGWIN but also expand variables

I need to expand variables before running the SCP command as a result I can't use single quote. If I run the script using double quotes in Powershell ISE it works fine.
But doesn't work if I run the script through command prompt.
I'm using zabbix to run the script which calls the script as [cmd /C "powershell -NoProfile -ExecutionPolicy Bypass -File .\myscript.ps1"]
Here is the code that needs to run SCP using Cygwin bash.
if ((test-path "$zipFile"))
{
C:\cygwin\bin\bash.exe -l "set -x; scp /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/"
}
Output:
/usr/bin/bash: set -x; /cygdrive/e/logs/myfolder/dir1/server.zip root#10.10.10.10:~/: No such file or directory
If I run the same command above in Cygwin manually it works.
I even tried to use bash -l -c but then the SSH session is stuck maybe because the root#10.10.10.10 becomes $1 according to the documentation.
Documentation link
-c If the -c option is present, then commands are read from
the first non-option argument command_string. If there are
arguments after the command_string, the first argument is
assigned to $0 and any remaining arguments are assigned to
the positional parameters. The assignment to $0 sets the
name of the shell, which is used in warning and error
messages.
Figured it out. It was halting when using bash -c was due to StrictHostKeyChecking, the known hosts thing (where you get a prompt to type yes/no). I set the -v switch to SCP and it showed me the Debug logs where it was halting.
Had to set scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null options.
The complete line now looks like the following:
c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v -i /cygdrive/c/cygwin/home/myuser/.ssh/id_rsa /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/")

Why do I keep getting a failed when trying to make a .cer for testing?

I'm trying to make a certificate for testing and I am using this site How to: Create Your Own Test Certificate as a tutorial on how to but when it comes time to enter in the password after you create the password it says failed in the command prompt why is that?
What I've done:
First, I open a command prompt and type in:
cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
Second, I type in
makecert -sv myPrivateKeyFile.pvk -n "TestCert" myCertFile.cer -b 01/01/2011 -e 01/01/2015 -r
Then I create the password then enter the password on the sencond pop up box and then it says failed in command prompt
When I enter in this again, the popup is asking for the password pops up first this time, and I enter it in and it says:
Error: CryptCertStrToNameW failed => 0x80092023 (-2146885597)
makecert -sv myPrivateKeyFile.pvk -n "TestCert" myCertFile.cer -b 01/01/2011 -e 01/01/2015 -r
I solved the problem by running the program as an administrator, but I had to change also the name of certificate in a LDAP style like this (CN=...):
makecert -sv myPrivateKeyFile.pvk -n "CN=TestCert" myCertFile.cer -b 01/01/2011 -e 01/01/2015 -r
This was a bit of a shotgun-debug for me. I started with this after reading the makecert guide:
makecert.exe /b 01/01/2014 /e 01/01/2114 /len 256 /m 1200 /n "CN=In-House-Software" /pe /r /sr localMachine Test_Cert.cer
There's two error-causing flaws here:
/e and /m likely are not supposed to overlap; seems kind of obvious, but they didn't document it or create an expressive error message for it.
/len 256 was an invalid value -- too low. This also didn't seem too obvious/documented/expressed in a good error message. 512 did work. I didn't bother finding the floor-value, but by default 1024 is used.
I ended up using this with success:
makecert.exe /b 01/01/2014 /e 01/01/2114 /len 512 /pe /r /n "CN=In-House-Software" /sr localMachine Test_Cert.cer
The reason why I was getting this error is, because I didn't right click on cmd when I ran it. So when you have this problem you need to right click on cmd and then run as administrator and see if that fixes your problem as it did mine.
I also received this error when I specified the CA name for a self-signed cert, in other words, if you specify the '-cy authority' and the '-r' options, then DON'T specify the CA name explicitly in the name via the -n option.
The issue was by the publisher name not starting with "CN=".
This fixed the issue, I didn't need to run as administrator.
For some reason I couldn't run makecert properly from Dropbox folders. So try moving it to a solid disk like C:\ and try again.

Why doesn't this work

Why doesn't this work? I want to copy the file to the remote machine and run it.
psexec \\epo-test -c C:\temp\Avmr64.msi /Silent
Heres the error I get
PsExec could not start Avmr64.msi on epo-test:
The system cannot find the file specified.
I am trying to copy Avmr64.msi over to epo-test and run a silent install but I get this
PsExec could not start Avmr64.msi on epo-test:
The system cannot find the file specified.
Any help would be great.
For those that are interested heres the answer
psexec -u domain\username \\system -s -i -d msiexec.exe /i "\\share\folder\msifile" /qb
I think what you're trying to run here is "/Silent". Try instead:
psexec \\epo-test -c C:\temp\Avmr64.msi "C:\temp\Avmr64.msi /Silent"
or something in that fashion.
Try the below ( untested ):
psexec \\epo-test -c C:\temp\Avmr64.msi "msiexec /i /passive Avmr64.msi"
http://forum.sysinternals.com/topic2542.html

Syntax error for makecert.exe tool

Did I missed anything during this?
I am running a this command from command prompt in Windows 7 to create a certificate but it is showing a syntax error.
Command I ran:
makecert.exe "c:\1\Test.cer" -a sha1 -n "CN=AppGuid" -sr LocalMachine -ss My -sky signature -pe -len 2048
Output I got:
Usage: MakeCert [ basic|extended options] [outputCertificateFile]
Basic Options
-sk
-ss
-sr
.
.
.
Did I missed anything during this?
Well, as you can see from the format it says MakeCert [basic] [output file].
The last item in your command is not the output file, so theres one thing wrong. As for the rest, I would assume that "C:\1\test.cer" is not an option, so that is probably wrong too.