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 
Romonov
  • 8,145
  • 14
  • 43
  • 55

1 Answers1

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
latkin
  • 16,402
  • 1
  • 47
  • 62
  • 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 – Romonov Oct 13 '15 at 18:46
  • Are you sure the tool is actually at that path? What does `dir .\makecert\makecert.exe` give you? – latkin Oct 13 '15 at 18:51
  • 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` – Romonov Oct 13 '15 at 19:03
  • 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 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 .pvk -n "cn=" .cer -r -eku 1.3.6.1.5.5.7.3.1` is running fine – Romonov Oct 13 '15 at 19:09
  • 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? – Romonov Oct 13 '15 at 19:21
  • Updated the question with the progress – Romonov Oct 13 '15 at 20:04
  • There were multiple issues with this question. Some of them have been resolved by the answers provided. So edited the question to focus on those issues and marked it answered. Moved the remaining issues to a [different question](http://stackoverflow.com/questions/33112917/powershell-executing-makecert-with-variables-giving-too-many-variables-error) Apologies for the confusion. – Romonov Oct 13 '15 at 21:31