0

The following are two commands that run in command prompt and create the required certificate files:

makecert –sv <cnName>.pvk -n "cn=<cnName>" <cnName>.cer -r -eku 1.3.6.1.5.5.7.3.1
pvk2pfx -pvk <cnName>.pvk -spc <cnName>.cer -pfx <cnName>.pfx -po <password>

I am trying to run the same commands in powershell using the following code:

$cnName = <sampleCnName> + ".com"
$pvkName = $cnName + ".pvk"
$cerName = $cnName + ".cer"
$pfxName = $cnName + ".pfx"
$certificatePassword = <password>

& "Makecert\makecert –sv $pvkName -n "cn=$cnName" $cerName -r -eku 1.3.6.1.5.5.7.3.1"
& "Makecert\pvk2pfx -pvk $pvkName -spc $cerName -pfx $pfxName -po $certificatePassword"

The current error is

& : The module 'Makecert' could not be loaded. For more information, run 'Import-Module Makecert'.

One issue is, while I run makecert and pvk2pfx command from the Makecert folder in the command prompt, I want to write the powershell script in the parent folder Makecert level. Wondering what is the correct way to do this.

Update: The following command worked in powershell:

$currentDirectory = Split-Path $Script:MyInvocation.MyCommand.Path
& "$currentDirectory\Makecert\makecert.exe" –sv actualCnName.pvk -n "cn=actualCnName" actualCnName.cer -r -eku 1.3.6.1.5.5.7.3.1 
2
  • @PetSerAl please see my comment for latkin's answer below. Commented Oct 13, 2015 at 19:17
  • @RyanBemrose, please see my comment for latkin's answer below Commented Oct 13, 2015 at 19:18

1 Answer 1

2

You have 2 issues right now -

  1. If you want to invoke a tool from a relative path based in the current directory, Powershell requires .\ qualification. i.e. makecert\makecert.exe won't work, you need .\makecert\makecert.exe.

  2. If you are using &, the subsequent string should contain only the path and tool name, not any arguments. i.e. & "sometool.exe -a foo -b bar" is wrong, & "sometool.exe" -a foo -b bar is right.

Also note that & is not needed unless the path and/or tool name contain spaces or other special characters, or the path has been stored in a string for other reasons. Given your sample code, it's not strictly needed here.

So I would recommend:

$cnName = <sampleCnName> + ".com"
$pvkName = $cnName + ".pvk"
$cerName = $cnName + ".cer"
$pfxName = $cnName + ".pfx"
$certificatePassword = <password>

.\makecert\makecert.exe –sv $pvkName -n "cn=$cnName" $cerName -r -eku 1.3.6.1.5.5.7.3.1
.\makecert\pvk2pfx.exe -pvk $pvkName -spc $cerName -pfx $pfxName -po $certificatePassword
Sign up to request clarification or add additional context in comments.

7 Comments

I tried: .\Makecert\makecert.exe –sv $pvkName -n "cn=$cnName" $cerName -r -eku 1.3.6.1.5.5.7.3.1 It is giving the error: The term '.\Makecert\makecert.exe' is not recognized as the name of a cmdlet, function... I also tried: $currentDirectory = Split-Path $Script:MyInvocation.MyCommand.Path .\$currentDirectory\Makecert\makecert.exe –sv $pvkName -n "cn=$cnName" $cerName -r -eku 1.3.6.1.5.5.7.3.1 '.\D:\CSoft\2015-08-11_Task1\Makecert\makecert.exe' is not recognized as the name of a cmdlet... error
Are you sure the tool is actually at that path? What does dir .\makecert\makecert.exe give you?
The command dir .\Makecert\makecert.exe is giving dir : Cannot find path 'C:\WINDOWS\system32\Makecert\makecert.exe' because it does not exist. The command dir .\$currentDirectory\Makecert\makecert.exe is giving Cannot find C:\WINDOWS\system32\D:\CSoft\2015-08-11_Task1\Makecert\makecert.exe because it does not exist. But the command: dir $currentDirectory\Makecert\makecert.exe is locating the makecert.exe file: -a---- 10/1/2012 9:13 AM 55632 makecert.exe
I tried the command: & "$currentDirectory\Makecert\makecert.exe" –sv $pvkName -n "cn=$cnName" $cerName -r -eku 1.3.6.1.5.5.7.3.1. While in powershell script, this is giving: Error: Too many parameters Usage: MakeCert [ basic|extended options] [outputCertificateFile] Basic Options -sk <keyName> Subject's key container name; To be created if not present -pe Mark generated private key as exportable..., the same command when run in the command prompt: Makecert\makecert.exe -sv <cnName>.pvk -n "cn=<cnName>" <cnName>.cer -r -eku 1.3.6.1.5.5.7.3.1 is running fine
If I replace the $pvkName, $cnName and $cerName with the actual strings themselves, the command & "$currentDirectory\Makecert\makecert.exe" –sv actualCnName.pvk -n "cn=actualCnName" actualCnName.cer -r -eku 1.3.6.1.5.5.7.3.1 is working in powershell. But I want to parameterize these and use the variable names $pvkName, $cnName and $cerName. Is there a way to do this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.