Share via


Powershell and keytool.exe

Question

Wednesday, May 1, 2019 11:32 PM

Hi,

1. I need testing if a certificate is already existing in cacerts.  I tested the below commandline but got Nothing of value...

$ret=start-process -FilePath "C:\Program Files (x86)\Java\jre1.8.0_201\bin\keytool.exe" -ArgumentList "-list -v -keystore C:\Program Files (x86)\Java\jre1.8.0_201\lib\security\cacerts" - passthru

Is it an easy way to test a certificate presence in cacerts?

2. I am looking to push our organisation certificate inside all cacerts on all computers. Somebody already did that with Scripting?

Thanks,

All replies (15)

Thursday, May 2, 2019 2:40 PM ✅Answered

Nope.  You need to learn how to parse. It is fundamental t programming.

https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-1-3/

\(ツ)_/


Thursday, May 2, 2019 12:32 AM

This is a PowerShell forum.  Your issue is with a Java tool.  Please post Java issues in a Java forum.

\(ツ)_/


Thursday, May 2, 2019 10:10 AM

Hi,

My first question is using PowerShell to run a commandline and I am looking to find a way to gather data in a PowerShell variable. So I am looking to find a way with PowerShell.

Thanks,


Thursday, May 2, 2019 10:13 AM

To return any output from an external command just assign it to a variable.

Your code already does that using the $ret" variable. That is all you can do.

\(ツ)_/


Thursday, May 2, 2019 10:17 AM

You may need t add "-NoNewWindow".

Also you don't need Start-Process or "PassThru"

${env:ProgramFiles(x86)}\Java\jre1.8.0_201\bin\keytool.exe -list -v -keystore "C:\Program Files (x86)\Java\jre1.8.0_201\lib\security\cacerts"

\(ツ)_/


Thursday, May 2, 2019 12:01 PM

Hi,

Your commandline is not working as there are variables and string. Why not using start-process?

thanks,


Thursday, May 2, 2019 12:06 PM

Then do it this way:

& "${env:ProgramFiles(x86)}\Java\jre1.8.0_201\bin\keytool.exe -list -v -keystore `"C:\Program Files (x86)\Java\jre1.8.0_201\lib\security\cacerts`""

\(ツ)_/


Thursday, May 2, 2019 12:17 PM

Hi,

When running only the first part of the commandline (they part of the keytool) without any parameters then I am seeing it working as it is asking me more parameters. As soon as I put the parameters part I get the below error.

& "${env:ProgramFiles(x86)}\Java\jre1.8.0_201\bin\keytool.exe -list -v -keystore `"C:\Program Files (x86)\Java\jre1.8.0_201\lib\security\cacerts`""

PS C:\WINDOWS\system32> & "${env:ProgramFiles(x86)}\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore `"${env:ProgramFiles(x86)}\Java\jre1.8.0_121\bin\lib\security\cacerts`""
& : Le terme «C:\Program Files (x86)\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore "C:\Program Files (x86)\Java\jre1.8.0_121\bin\lib\security\cacerts"» n'est pas reconnu comme nom 
d'applet de commande, fonction, fichier de script ou programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès est correct et 
réessayez.
Au caractère Ligne:1 : 3
+ & "${env:ProgramFiles(x86)}\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...curity\cacerts":String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Thursday, May 2, 2019 12:20 PM

The error tells you what is wrong.

ObjectNotFound: (C:\Program File...curity\cacerts"

\(ツ)_/


Thursday, May 2, 2019 12:29 PM

Hi,

You were right. Wrong path for CACerts but even after changing the path:

PS C:\WINDOWS\system32> & "${env:ProgramFiles(x86)}\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore `"C:\Program Files (x86)\Java\jre1.8.0_121\lib\security\cacerts`""
& : Le terme «C:\Program Files (x86)\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore "C:\Program Files (x86)\Java\jre1.8.0_121\lib\security\cacerts"» n'est pas reconnu comme nom 
d'applet de commande, fonction, fichier de script ou programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès est correct et 
réessayez.
Au caractère Ligne:1 : 3
+ & "${env:ProgramFiles(x86)}\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...curity\cacerts":String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I double checked the path for the keytool and cacerts. Both are correct now.


Thursday, May 2, 2019 12:49 PM

Then try it like this.

& 'C:\"Program Files (x86)"\Java\jre1.8.0_121\bin\keytool.exe -list -v -keystore "C:\Program Files (x86)\Java\jre1.8.0_121\lib\security\cacerts"'

\(ツ)_/


Thursday, May 2, 2019 2:01 PM

Hi,

OK this is working:

$ret=& "C:\Program Files (x86)\Java\jre1.8.0_121\bin\keytool.exe" -storepass changeit -list -v -keystore "C:\Program Files (x86)\Java\jre1.8.0_121\lib\security\cacerts"

How may I find a particular string in $ret

For each ($rret in $ret) {

...

}

No faster way?


Thursday, May 2, 2019 2:22 PM

You have to parse the string.

\(ツ)_/


Thursday, May 2, 2019 2:34 PM

Hi,

I need to test each line of the array? No faster way?

Thanks,


Tuesday, December 3, 2019 12:06 AM

you may try below 

$x = '-keystore "' +  $env:java_Home + '\lib\security\cacerts"'

$ret= (start-process "$env:Java_home\bin\keytool.exe" -ArgumentList '-list', '-v', '-storepass changeit', $x -PassThru -RedirectStandardOutput results.txt)

Once it is executed, you will need to read thru the results.txt file to check the existence of certificate. 

I haven't tested yet, but you may refer to https://www.powershellgallery.com/packages/PSCertUtils/0.2.1/Content/PSCertUtils.Java.psm1 to see if this helps.