Powershell 和 System.Security.Cryptography.X509Certificates.X509Certificate2

发布于 2024-11-24 00:57:11 字数 590 浏览 1 评论 0原文

当我运行 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**

我做错了什么?

i'm getting this error when i run the system.security namespace. This is what i am running after

$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**

What am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攀登最高峰 2024-12-01 00:57:11

尝试运行此命令以查看是否已加载 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

Try running this to see if you have the System.dll loaded (should be by default):

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

If it is loaded then this command should show the X509Certificate2 type:

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

If the System.dll isn't loaded (which would be odd) try loading it:

Add-Type -AssemblyName System

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

腹黑女流氓 2024-12-01 00:57:11

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

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

FYI ... I got error:

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

when using:

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

However, I had no error creating the collection earlier on in my script:

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

To make the error go away I had to include the following at some point earlier on:

Add-Type -AssemblyName System.Security
烟凡古楼 2024-12-01 00:57:11

我已经解决了我的问题。这很容易:

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

cd\ 是必要的

I've solved my problem. It's easily:

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

cd\ is necessary

温柔少女心 2024-12-01 00:57:11

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

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

它将不会返回 System.Security,但如果您随后键入此命令并让智能感知执行其操作:

[System.

您可以再次运行此命令:

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

然后它将返回 <代码>系统.安全。因此,这就是为什么您可以编写一个运行良好的脚本,然后稍后重新访问它,但它却损坏了。使用智能感知并不能修复你的脚本,而是你必须添加这一行:

Add-Type System.Security

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

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

I ran into this in the ISE (but seems to apply to the normal command window too) and it seems that using autocomplete will automatically Add-Type for whatever you're looking for. If you start a new instance and run:

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

it will not return System.Security, but if you then type this and let intellisense do its thing:

[System.

You can then run this again:

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

And it will then return System.Security. So this is why you can write a script that works fine, and then revisit it later and it's broken. Using intellisense doesn't fix your script though, instead you have to add this line:

Add-Type System.Security

Or whatever library is not getting auto-added (it seems to need the dll filename, e.g. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Security.dll).

I'm pretty sure IseSteroids (a paid ISE add-in) can detect this, maybe others as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文