2016-03-08 44 views
0

我正在嘗試搜索Active Directory並獲取用戶名的報告以及證書何時到期。這是我從其他渠道拼湊出來的代碼。當我在PowerShell窗口中手動運行它時,它會顯示我想要的NotAfter日期。Export-Csv在處理日期時拋出錯誤

enter image description here

但是,當我嘗試寫一個CSV文件,它告訴我NotAfter值是「System.Object的[]」。一個網站提到使用JSON格式,但這不是很友善的眼睛。任何援助將不勝感激。

$cert = Get-ADUser -LDAPFilter $StrFilter -server $domain -searchbase $searchOU -Properties "Certificates" 
Foreach ($i in $cert) 
{ 
    $tempobj = $cert.Certificates | foreach { New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $_ } 

    If ($Cert.Certificates.Handle -eq $null) 
    { 
     $Rep = "" | Select "Account", "NotAfter" 
     $Rep."Account" = $cert.name 
     $Rep."NotAfter" = "N/A" 
     $Myobj += $Rep 
     $Rep = $null 
    } 
    Else 
    { 
     $Rep = "" | Select "Account", "NotAfter" 
     $Rep."Account" = $cert.name 
     $Rep."notAfter" = $tempobj.NotAfter 
    } 

    $Myobj += $Rep 
    $Rep = $null 
} 

#After the loop, export the array to CSV 
$Myobj | sort | export-csv -Path C:\xtemp\scripts\pshell\UserCerts.csv -notype 
+2

您的代碼不作任何意義。爲什麼你使用'foreach($ i $ cert)',然後*從不使用'$ i' *?而是指定'$ tempobj = $ cert.Certificates | [...]'。不應該是'$ tempobj = $ i.Certificates | [...]'? –

+0

爲草率代碼道歉。我一直在搜索互聯網尋找方法,這是一些混合的結果。我有類似的東西編碼,但仍然得到相同的結果,當推到csv。 – user3067193

回答

1

你需要一些重大的更新在你的代碼... 這應該是一個起點:

$cert = Get-ADUser -LDAPFilter $StrFilter -server $domain -searchbase $searchOU -Properties "Certificates" 
$cert = $cert | ? {$_.Certificates} 
$Myobj = @() 

Foreach ($i in $cert) 
    { 
    $tempobj = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $i.Certificates 

    $Rep = "" | Select Account, NotAfter 

    If ($i.Certificates.Handle -eq $null) 
     { 
     $Rep.Account = $i.name 
     $Rep.NotAfter = "N/A" 
     } 
    Else 
     { 
     $Rep.Account = $i.name 
     $Rep.NotAfter = $tempobj.NotAfter.ToString() 
     } 

    $Myobj += $Rep 
    } 
+0

這工作。我以爲我之前曾嘗試過「ToString()」,我認爲我的草率代碼阻礙了我的發展。 – user3067193

相關問題