0
$Srv ='10.101.22.82' #remote server
$key = "SOFTWARE\\Microsoft\\SystemCertificates\\MY\\Certificates"
$type = [Microsoft.Win32.RegistryHive]::LocalMachine
$regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $Srv)
$regKey = $regKey.OpenSubKey($key)
Write-Host "Sub Keys"
Write-Host "--------"
Foreach($sub in $regKey.GetSubKeyNames())
{
 #open the subkey here and parse the contents
 $myStr = "SOFTWARE\\Microsoft\\SystemCertificates\\MY\\Certificates\\"+$sub
 $regKey2 = $regKey.OpenSubKey($myStr)
 $bytes = $regKey2.GetValue($sub).Blob
 echo $bytes
 $cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]$bytes
 $cert | Select Subject, Issuer, NotBefore, NotAfter, Thumbprint, SerialNumber
} 

I am trying to read remote registry and fetch SSL certificates using PowerShell. What am I missing? Pointers appreciated, thanks

jharkhand
  • 1
  • 2

1 Answers1

0

Do you have remoting enabled on the remote hosts you're querying? If so, something like

Invoke-Command -Computername COMPUTER -ScriptBlock {Get-ChildItem Cert:\LocalMachine\My}

would be a lot cleaner.

Adam Bertram
  • 3,858
  • 4
  • 22
  • 28
  • I do not have remoting enabled and it will likely not be an option in the client's environment... The $sub echoes the subkey names correctly though – jharkhand Oct 23 '14 at 16:09
  • What kind of output are you looking for? – Adam Bertram Oct 23 '14 at 18:31
  • If the script works correctly it will respond with all the certificates installed on the remote system displaying the following attributes. Subject : CN=abc.def.com, OU=Food Services, O=MSFT Technologies, L=Rome, S=GA, C=US Issuer : CN=demo-GA0001-CA, DC=demo, DC=net NotBefore : 8/5/2014 8:40:18 AM NotAfter : 8/5/2016 8:50:18 AM Thumbprint : C845369952DD9CX920F946422BB816623C640D0B SerialNumber : 2310DD6D0DD000880049 – jharkhand Oct 23 '14 at 18:44
  • I edited my answer. Will that work? That will display all the certs in the Personal cert store of the local machine. – Adam Bertram Oct 23 '14 at 22:10
  • I tried the script and it seems to work if there are no access restrictions. The URL below seems promising: http://blogs.technet.com/b/heyscriptingguy/archive/2013/09/29/weekend-scripter-max-out-powershell-in-a-little-bit-of-time-part-2.aspx – jharkhand Oct 27 '14 at 22:30