I am trying to write some keytool wrappers in Powershell for managing keystores. The issue I am facing, is that in Powershell, when running a command similar to:
$cmd = "& " + $keytoolexe + " -import " + $CAswitch +
" -alias " + $alias +
" -keystore " + $keystore + " -storepass " + $storepass +
" -file " + $file +
" -noprompt *>&1"
$result = Invoke-Expression -Command $cmd
As you can see I am redirecting everything to standard ouptut, as I want to store that in a variable to evaluate the result of the command.
The problem is that when keytool.exe returns a successful message, powershell somehow reads it as error.
I tried to run the block-code, itself, and I did not get any, error. However after I put this code inside a function and I invoked the function with parameter -ErrorAction stop, i got this error.
keytool.exe : Certificate was added to keystore
At line:1 char:1
+ & "C:\Program Files\Emulex\OCM for VMware\JRE\bin\keytool.exe" -impor ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Certificate was added to keystore:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
So somehow ErrorAction Stop seems to make the standard ouptut redirection disappear.