【问题标题】:Powershell and System.Security.Cryptography.X509Certificates.X509Certificate2Powershell 和 System.Security.Cryptography.X509Certificates.X509Certificate2
【发布时间】:2011-10-05 09:31:53
【问题描述】:

我在运行 system.security 命名空间时收到此错误。这就是我追求的目标

$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")

New-Object: Cannot find type [System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")]: make sure the assembly containing this type is loaded. 
At line:1 char:19
    + $cert = New-Object <<<<
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand**

我做错了什么?

【问题讨论】:

  • 命令看起来没问题。我无法重现 PowerShell v1 或 v2 中的错误。

标签: security powershell certificate x509certificate x509


【解决方案1】:

仅供参考...我收到错误:

Unable to find type [System.Security.Cryptography.x509Certificates.X509Certificate2UI]

使用时:

$certSelect = [System.Security.Cryptography.x509Certificates.X509Certificate2UI]::SelectFromCollection($certCollection, $title, $msg, 0)

但是,我之前在脚本中创建集合时没有出错:

$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection

为了消除错误,我必须在早些时候添加以下内容:

Add-Type -AssemblyName System.Security

【讨论】:

    【解决方案2】:

    我在 ISE 中遇到了这个问题(但似乎也适用于普通命令窗口),使用自动完成功能似乎会自动 Add-Type 来查找您要查找的内容。如果您启动一个新实例并运行:

    [AppDomain]::CurrentDomain.GetAssemblies() | grep Security
    

    它不会返回System.Security,但是如果你输入这个并让智能感知来做它的事情:

    [System.
    

    然后您可以再次运行它:

    [AppDomain]::CurrentDomain.GetAssemblies() | grep Security
    

    然后它将返回System.Security。所以这就是为什么你可以编写一个运行良好的脚本,然后再重新访问它,它就坏了。但是,使用智能感知并不能修复您的脚本,而是您必须添加以下行:

    Add-Type System.Security
    

    或者任何没有自动添加的库(它似乎需要 dll 文件名,例如C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Security.dll)。

    我很确定 IseSteroids(付费 ISE 插件)可以检测到这一点,也许其他人也可以。

    【讨论】:

      【解决方案3】:

      尝试运行它来查看您是否加载了 System.dll(应该默认加载):

      [AppDomain]::CurrentDomain.GetAssemblies() | 
          Where {$_.Location -match '\\System\\'}
      

      如果已加载,则此命令应显示 X509Certificate2 类型:

      [AppDomain]::CurrentDomain.GetAssemblies() | 
      Where {$_.Location -match '\\System\\'} | 
      %{$_.GetExportedTypes()} | Where {$_.Name -match 'X509Cert'}
      

      如果 System.dll 未加载(这很奇怪),请尝试加载它:

      Add-Type -AssemblyName System
      

      见:http://technet.microsoft.com/en-us/library/hh849914.aspx

      【讨论】:

        【解决方案4】:

        我已经解决了我的问题。很简单:

        cd\
        $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")
        

        cd\ 是必须的

        【讨论】:

        • 是的,你这里有一个未找到类型的错误,不是路径问题。无论如何,您不应该将文件放入 c:\,但我们假设您确实找到了实际错误!在 Powershell 中:使用 resolve-path 解析文件的完整路径,并将其传递给方法。请记住,每当在 powershell 中进行方法调用时,cwd 并不总是您认为的那样。 $pwd 不等于运行时工作目录(甚至不包括提供程序的参数。)
        猜你喜欢
        • 2021-07-07
        • 1970-01-01
        • 2015-10-18
        • 2012-02-25
        • 2021-05-31
        • 2015-06-11
        • 1970-01-01
        • 2020-07-17
        • 2013-12-24
        相关资源
        最近更新 更多