简体   繁体   English

在Powershell中运行命令提示符命令

[英]Run command prompt commands in powershell

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: 我正在尝试使用以下代码在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 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. 一个问题是,当我在命令提示符下的Makecert文件夹中运行makecert和pvk2pfx命令时,我想在父文件夹Makecert级别中写入powershell脚本。 Wondering what is the correct way to do this. 想知道什么是正确的方法。

Update: The following command worked in powershell: 更新:以下命令在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 

You have 2 issues right now - 您现在有2期-

  1. If you want to invoke a tool from a relative path based in the current directory, Powershell requires .\\ qualification. 如果要从当前目录中的相对路径调用工具,则Powershell需要.\\限定。 ie makecert\\makecert.exe won't work, you need .\\makecert\\makecert.exe . makecert\\makecert.exe无法正常工作,您需要.\\makecert\\makecert.exe

  2. If you are using & , the subsequent string should contain only the path and tool name, not any arguments. 如果使用& ,则后续字符串应仅包含路径和工具名称, 而不包含任何参数。 ie & "sometool.exe -a foo -b bar" is wrong, & "sometool.exe" -a foo -b bar is right. & "sometool.exe -a foo -b bar"错误, & "sometool.exe" -a foo -b bar是正确的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell:如何从PowerShell以管理员身份运行远程命令提示符命令? - PowerShell : How to run remote command prompt commands as admin from PowerShell? Powershell中命令提示符命令的错误处理 - Error handling of command prompt commands in Powershell 如何在特殊的PowerShell 4命令提示符处链接命令? - How to Chain Commands at a Special PowerShell 4 Command Prompt? 从命令提示符运行提升的 powershell 命令 - Run elevated powershell command from command prompt 无法通过命令提示符运行 powershell 命令 - Cannot run powershell command via command prompt 使用PowerShell在命令提示符中运行war文件 - Run a war file in command Prompt using PowerShell 命令提示符:运行PowerShell时拒绝访问 - Command Prompt: access denied when run PowerShell Powershell中的批处理文件命令在其他命令提示符下执行 - batch file commands in powershell execute in a different command prompt Active Directory命令可在PowerShell下运行,但不能在命令提示符下运行 - Active Directory commands work under PowerShell but not with command prompt Powershell命令之一无法在脚本中运行,但是如果在命令提示符下运行,则可以运行 - One of the Powershell Command is not working in a script but works if run on the command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM