Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and System.Security.Cryptography.X509Certificates.X509Certificate2

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?

like image 695
NieAR Avatar asked Jan 30 '26 04:01

NieAR


2 Answers

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

like image 99
Keith Hill Avatar answered Jan 31 '26 21:01

Keith Hill


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

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

cd\ is necessary

like image 28
NieAR Avatar answered Jan 31 '26 21:01

NieAR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!