Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   AutoIt (http://forum.oszone.net/forumdisplay.php?f=103)
-   -   [решено] Сертификаты компьютера (http://forum.oszone.net/showthread.php?t=321861)

HFShak 19-12-2016 12:24 2696844

Сертификаты компьютера
 
Добрый день!
Кто-знает как посмотреть сертификаты компьютера через autoit?
Нужно получать S/N и контейнер сертификата.

Аналог кода на powershell:
Код:

$MyCerts = dir Cert:\LocalMachine\My
Foreach($MyCert in $MyCerts)
{
    $MyCert.SerialNumber
    $MyCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
}


Mike909 19-12-2016 16:44 2696921

Код:

#include<array.au3>

_ArrayDisplay(_GetCert()) 

Func _GetCert()

    $psLine = "$MyCerts = dir Cert:\LocalMachine\My; Foreach($MyCert in $MyCerts) {  $MyCert.SerialNumber;  $MyCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName; }"

  $iPID = Run(@ComSpec & " /c Powershell " & $psLine, "", @SW_HIDE , $stdout_child)

  $sOutput = ""

          While 1
                $sOutput &= StdoutRead($iPID)
                If @error Then
                    ExitLoop
                EndIf
          WEnd

  $aRtn = stringsplit($sOutput , @LF , 2)
  _ArrayDelete($aRtn , ubound($aRtn) - 1)
  If ubound($aRtn) < 1 then _ArrayAdd($aRtn , "No Match Found")
  Return $aRtn

EndFunc


Creat0R 20-12-2016 09:20 2697055

Код:

#include <Array.au3>
#include <Date.au3>

Global $oCertStore = ObjCreate('CAPICOM.Store')
$oCertStore.Open(1, 'Root' , 0) ;Change 'Root' to 'my' for user's local machine store
Global $oCerts = $oCertStore.Certificates
Global $aCerts[$oCerts.Count + 1][7] = [[$oCerts.Count, 'IssuerName', 'SerialNumber', 'SubjectName', 'Version', 'ValidFromDate', 'ValidToDate']]
Global $iC = 1

For $oCert In $oCerts
    $aCerts[$iC][1] = $oCert.IssuerName
    $aCerts[$iC][2] = $oCert.SerialNumber
    $aCerts[$iC][3] = $oCert.SubjectName
    $aCerts[$iC][4] = $oCert.Version
    $aCerts[$iC][5] = _DateFormat($oCert.ValidFromDate)
    $aCerts[$iC][6] = _DateFormat($oCert.ValidToDate)

    $iC += 1
Next

_ArrayDisplay($aCerts)

Func _DateFormat($sDate)
    Local $aDate = StringRegExp($sDate, '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', 3)

    If UBound($aDate) < 6 Then
        Return SetError(1, 0, $sDate)
    EndIf

    Return StringFormat('%02i/%02i/%04i %02i:%02i:%02i', $aDate[2], $aDate[1], $aDate[0], $aDate[3], $aDate[4], $aDate[5])
EndFunc

https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

HFShak 20-12-2016 11:02 2697067

Странно, но у меня возникает ошибка:
Код:

C:\TEMP\t6.au3 (5) : ==> Variable must be of type "Object".:
$oCertStore.Open(1, 'My' , 0)
$oCertStore^ ERROR


Mike909 20-12-2016 11:42 2697078

Добавил получение PrivateKey:

Код:

....
Global $oCertStore = ObjCreate('CAPICOM.Store')
;1 - CAPICOM_LOCAL_MACHINE_STORE
;2 - CAPICOM_CURRENT_USER_STORE
$oCertStore.Open(1, 'My' , 0) ; Change 'Root' to 'my' for user's local machine store    ; First param: 1-  Local Machine ; 2 - Current User
Local $oCerts = $oCertStore.Certificates
Local $aCerts[$oCerts.Count + 1][8] = [[$oCerts.Count, 'IssuerName', 'SerialNumber', 'SubjectName', 'Version', 'ValidFromDate', 'ValidToDate', 'PrivateKey UniqueContainerName']]
Local $iC = 1
;Local $oPriv = $oCertStore.PrivateKey.UniqueContainerName
For $oCert In $oCerts
    $aCerts[$iC][1] = $oCert.IssuerName
    $aCerts[$iC][2] = $oCert.SerialNumber
    $aCerts[$iC][3] = $oCert.SubjectName
    $aCerts[$iC][4] = $oCert.Version
    $aCerts[$iC][5] = _DateFormat($oCert.ValidFromDate)
    $aCerts[$iC][6] = _DateFormat($oCert.ValidToDate)
    If $oCert.HasPrivateKey() Then
        $aCerts[$iC][7] = $oCert.PrivateKey.UniqueContainerName
    EndIf
    $iC += 1
Next

....

https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

Creat0R 20-12-2016 14:13 2697148

Код:

#include <Array.au3>

Global Const $CAPICOM_LOCAL_MACHINE_STORE = 1
Global Const $CAPICOM_CURRENT_USER_STORE = 2

Global $iOption = $CAPICOM_LOCAL_MACHINE_STORE ;Change only this to one of above options

$aCerts = _Certificates_Get($iOption)

If @error Then
    Exit MsgBox(16, @ScriptName, 'Unable to create CAPICOM.Store object' & @CRLF & @CRLF & ' ==> EXIT')
EndIf

_ArrayDisplay($aCerts)

Func _Certificates_Get($iOption)
    Local Const $sOption = ($iOption = $CAPICOM_CURRENT_USER_STORE ? 'My' : 'Root')

    Local $oCertStore = ObjCreate('CAPICOM.Store')

    If Not IsObj($oCertStore) Then
        Return SetError(1, 0, 0)
    EndIf

    $oCertStore.Open($iOption, $sOption, 0)
    Local $oCerts = $oCertStore.Certificates

    If Not IsObj($oCerts) Then
        Return SetError(2, 0, 0)
    EndIf

    Local $iC = 1
    Local $aCerts[$oCerts.Count + 1][8] = [[$oCerts.Count, 'IssuerName', 'SerialNumber', 'SubjectName', 'Version', 'ValidFromDate', 'ValidToDate', 'PrivateKey UniqueContainerName']]

    For $oCert In $oCerts
        $aCerts[$iC][1] = $oCert.IssuerName
        $aCerts[$iC][2] = $oCert.SerialNumber
        $aCerts[$iC][3] = $oCert.SubjectName
        $aCerts[$iC][4] = $oCert.Version
        $aCerts[$iC][5] = _Certificates_DateFormat($oCert.ValidFromDate)
        $aCerts[$iC][6] = _Certificates_DateFormat($oCert.ValidToDate)

        If $oCert.HasPrivateKey() Then
            $aCerts[$iC][7] = $oCert.PrivateKey.UniqueContainerName
        EndIf

        $iC += 1
    Next

    Return $aCerts
EndFunc

Func _Certificates_DateFormat($sDate)
    Local $aDate = StringRegExp($sDate, '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', 3)

    If UBound($aDate) < 6 Then
        Return SetError(1, 0, $sDate)
    EndIf

    Return StringFormat('%02i/%02i/%04i %02i:%02i:%02i', $aDate[2], $aDate[1], $aDate[0], $aDate[3], $aDate[4], $aDate[5])
EndFunc



Время: 11:29.

Время: 11:29.
© OSzone.net 2001-