Home > Back-end >  Adding local user SSL certificate (.crt) to all Java stores on Windows PC
Adding local user SSL certificate (.crt) to all Java stores on Windows PC

Time:06-12

I wrote the following windows power shell script in order to add my crt SSL certificate to all java cacert stores

$certs = @(Get-Childitem -Path "C:\Program files" -Include *cacerts* -Exclude *.pem -File -Recurse -ErrorAction SilentlyContinue);$keytool = Get-Childitem -Path "C:\Program files" -filter keytool.exe -Recurse -ErrorAction SilentlyContinue | Select -First 1; foreach ( $cert in $certs ){& "$keytool" -import -trustcacerts -alias mycert-ca$(date -format dMyyhm) -file C:\Path\to\my-crt.crt -keystore $cert -storepass changeit -noprompt}

the problem is that when I try to run it on anything more than program files the search takes a very long time. when I am searching in the command prompt with the following command dir c:\ /s /b | findstr -i cacerts I get results for the whole c drive within a minute.

is there any equivalent command for ps?

CodePudding user response:

I post here my script updated after the comments I got on my question I checked it on my pc and it worked perfectly.

$certs = @(Get-Childitem –Path "C:\Program files", "C:\Program Files (x86)" -Filter cacerts -Recurse -ErrorAction SilentlyContinue);$keytool = Get-Childitem -Path "C:\Program files", "C:\Program Files (x86)" -Filter keytool.exe -Recurse -ErrorAction SilentlyContinue | Select -First 1; foreach ( $cert in $certs ){& "$keytool" -import -trustcacerts -alias myCert$(date -format dMyyhm) -file C:\Path\to\CA\myCertificate.crt -keystore $cert -storepass changeit -noprompt}

make sure to change the -file C:\Path\to\CA\myCertificate.crt to your certificate path. and the -alias myCert$(date -format dMyyhm) to a name of your choice.

The $(date -format dMyyhm) is there in case you want to have an option to update the certificate without deleting the old on, if that is not the case you can remove this part.

in the enclosed version I was searching only in the two program files directories because as far as I know, that's where all Java containing files are located but you can change the -path flag to point to "C:\" or even "\" for me it took 2 minutes on a 1TB entire drive.

  • Related