简体   繁体   English

检查字符串是否为X509Store路径或PFX文件

[英]Check if string is X509Store path or PFX file

I need to check if a string is a certificate store (eg. "Cert:\\CurrentUser\\My" ) or a pfx file path (eg. "D:\\\\PFXfiles\\self-signed.pfx" ) 我需要检查字符串是证书存储区(例如"Cert:\\CurrentUser\\My" )还是pfx文件路径(例如"D:\\\\PFXfiles\\self-signed.pfx"

Which method is better to use and why? 哪种方法更好,为什么? What are the cons/pros for each? 每个优点/缺点是什么? Is there a better method? 有没有更好的方法? Method 1: 方法1:

if ($certLocation.ToUpper().Contains(".PFX"))
{
    #it's a .pfx file
}
else
{
    #it's a cert store
}

Method 2: 方法2:

if ((Resolve-Path -LiteralPath $certLocation).Provider.Name -eq "FileSystem")
{
    #it's a .pfx file
}
elseif ((Resolve-Path -LiteralPath $certLocation).Provider.Name -eq "Certificate"
{
    #it's a cert store
}

I'd use Split-Path : 我会使用Split-Path

switch ((Split-Path $certLocation -Qualifier)) {
  'cert:' { 'cert store' }
  'c:'    { 'file path' }
  default { Write-Error "invalid provider: $_" }
}

Check the extension inside the 'file path' script block if required. 如果需要,请检查“文件路径”脚本块中的扩展名。


you should see magic number of file , I recommend to you use file command exist in linux and the programmer provide for windows see this link 您应该看到神奇的文件数量 ,我建议您使用Linux中存在的file命令,并且程序员为Windows提供该链接
see my example 看我的例子

C:\Program Files (x86)\GnuWin32\bin>file.exe c:\Users\soheil\Desktop\1.pfx
c:\Users\soheil\Desktop\1.pfx; data

C:\Program Files (x86)\GnuWin32\bin>file.exe c:\Users\soheil\Desktop\2.pfx
c:\Users\soheil\Desktop\2.pfx; empty

or like this 或像这样

C:\Program Files (x86)\GnuWin32\bin>file.exe c:\a.txt
c:\a.txt; UTF-8 Unicode (with BOM) English text, with very long lines, with CRLF
 line terminators

first 1.pfx i create self sign with IIS 第一个1.pfx我使用IIS创建自签名
second 2.pfx i rename txt file to 2.pfx 第二个2.pfx我将txt文件重命名为2.pfx
if you want exactly understand what file is you should use file command for see magic number 如果您想完全了解什么文件,则应使用file命令查看幻数

To me, testing the string would be better because it's more efficient to just manipulate the string vs resolving the path, creating another object and then reading a property of on that object, but in reality it's not going to change anything. 对我而言,测试字符串会更好,因为仅操作字符串而不是解析路径,创建另一个对象然后读取该对象的属性会更有效,但实际上,它不会更改任何内容。 I'd do it a little differently though. 我会做一些不同的事情。

if ($certLocation.Split(":")[0] -like "cert") { 
    #it's a cert store
}
else {
    #it's a pfx
}

I'll chime in... If you are testing a string to see where the path lies use the Resolve-Path cmdlet, and select the Provider property. 我会鸣叫...如果您要测试字符串以查看路径在哪里,请使用Resolve-Path cmdlet,然后选择Provider属性。

$StringPath = "Cert:\CurrentUser\my","C:\Temp\fakecert.pfx"

Switch($StringPath){
    {(Resolve-Path $_|Select -Expand Provider).tostring() -eq "Microsoft.PowerShell.Security\Certificate"} {"$_ is in the Certificate Store";continue}
    {(Resolve-Path $_|Select -Expand Provider).tostring() -eq "Microsoft.PowerShell.Core\FileSystem"} {"$_ is in the File System"}
}

Cert:\CurrentUser\my is in the Certificate Store
C:\Temp\fakecert.pfx is in the File System

That way PowerShell will tell you who it used to resolve the path. 这样,PowerShell会告诉您它用来解析该路径的人。 This will throw errors if you provide invalid paths, but should give you accurate info as to where items are stored. 如果您提供无效的路径,这将引发错误,但应为您提供有关项目存储位置的准确信息。 Error catching could be added to catch invalid paths, but that's up to you. 可以添加错误捕获来捕获无效路径,但这取决于您。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM