将绝对路径传递给命令
问题描述:
我想导入证书和CA以在PowerShell中使用certutil.exe进行存储。 这是我的脚本:将绝对路径传递给命令
$appDir="C:\Program Files\My App"
$certFilespec = $appDir + "\mycert.pfx"
certutil -p '""' -importPFX $certFilespec
$certFilespec = $appDir + "\myca.crt"
certutil -f -addStore Root $certFilespec
一切,但第三行成功执行。错误是:
PS C:\> certutil -p '""' -importPFX $certFilespec
CertUtil: -importPFX command FAILED: 0x80070002 (WIN32: 2)
CertUtil: The system cannot find the file specified.
PS C:\>
当我使用字符串代替$ certFilespec的
certutil -p '""' -importPFX "C:\Program Files\My App\mycert.pfx"
certutil -f -addStore Root "C:\Program Files\My App\myca.crt"
一切顺利执行。我还发现,当我使用相对路径它工作正常
PS C:\> cd '.\Program Files\My App'
$certFilespec=".\mycert.pfx"
certutil -p '""' -importPFX $certFilespec
CertUtil: -importPFX command completed successfully
PS C:\Program Files\My App>
一切工作正常。所以我想在使用绝对路径时引用会有一些问题。我不明白的是,它对于相同的命令只有不同的选项(-addStore/-importPFX)有不同的作用。
我导入的文件是PKCS12证书+私钥(.pfx文件)。和CA的证书(.crt文件)。但是这不应该起到任何作用。
答
最好的猜测。 当它生成$ certFilespec时,它会生成没有注释的字符串,因此该命令将看到C:\ Program并发出错误消息。
$appDir="C:\Program Files\My App"
$certFilespec = $appDir + "\mycert.pfx"
$certFilespec
C:\ Program Files文件\我的应用\ mycert.pfx
你可以尝试
$appDir='"C:\Program Files\My App\'
$certFilespec = $appDir + 'mycert.pfx"'
运行时产生
$certFilespec
“C:\ Program Files文件\ My App \ mycert.pfx“
答
T RY修改此行:
certutil -p '""' -importPFX $certFilespec
到
certutil -p '""' -importPFX "$certFilespec"
既然你在你的路径有一个空间,它打破了单一的路径参数划分为多个参数。