2015-10-13 205 views
0

提示命令以下是在命令提示符下运行,并创建所需的证书文件两个命令:运行命令在PowerShell中

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> 

我尝试使用下面的代码在PowerShell中运行相同的命令:

$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 module 'Makecert' could not be loaded. For more information, run 'Import-Module Makecert'. 

的一个问题是,当我运行makec ert和pvk2pfx命令来自命令提示符下的Makecert文件夹,我想将powershell脚本写入父文件夹Makecert级别。想知道做这件事的正确方法是什么。

更新: 下面的命令在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 
+0

@PetSerAl请参阅我的评论下面latkin的答案。 – Romonov

+0

@RyanBemrose,请参阅我对Latkin的回答下面的评论 – Romonov

回答

2

你有2个问题,现在 -

  1. 如果你想调用基于当前的相对路径的工具目录,Powershell需要.\资格。即makecert\makecert.exe将不起作用,您需要.\makecert\makecert.exe

  2. 如果使用&,随后的字符串应当只包含路径和工具的名称,任何参数。即& "sometool.exe -a foo -b bar"是错误的,& "sometool.exe" -a foo -b bar是正确的。

还要注意,不需要&除非路径和/或工具的名称包含空格或其他特殊字符或路径已被存储在其他原因的字符串。鉴于你的示例代码,这里并不是必须的。

因此,我建议:

$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 
+0

我试过了: 。\ Makecert \ makecert.exe -sv $ pvkName -n“cn = $ cnName”$ cerName -r -eku 1.3.6.1.5.5.7.3。 1 它给出的错误: 术语'。\ Makecert \ makecert.exe'不被识别为cmdlet的名称,函数... 我也尝试过: $ 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'未被识别为cmdlet的名称...错误 – Romonov

+0

您确定该工具实际上位于该路径吗? 'dir。\ makecert \ makecert.exe'给你什么? – latkin

+0

命令'dir。\ Makecert \ makecert.exe'给出'dir:无法找到路径'C:\ WINDOWS \ system32 \ Makecert \ makecert.exe',因为它不存在。' 命令'dir。\ $ currentDirectory \ Makecert \ makecert.exe'给出'无法找到C:\ WINDOWS \ system32 \ D:\ CSoft \ 2015-08-11_Task1 \ Makecert \ makecert.exe'因为它不存在。但是命令:'dir $ currentDirectory \ Makecert \ makecert.exe'正在查找makecert.exe文件: '-a ---- 2012年10月1日上午9:13 55632 makecert.exe' – Romonov