0
我正在嘗試搜索Active Directory並獲取用戶名的報告以及證書何時到期。這是我從其他渠道拼湊出來的代碼。當我在PowerShell窗口中手動運行它時,它會顯示我想要的NotAfter
日期。Export-Csv在處理日期時拋出錯誤
但是,當我嘗試寫一個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
您的代碼不作任何意義。爲什麼你使用'foreach($ i $ cert)',然後*從不使用'$ i' *?而是指定'$ tempobj = $ cert.Certificates | [...]'。不應該是'$ tempobj = $ i.Certificates | [...]'? –
爲草率代碼道歉。我一直在搜索互聯網尋找方法,這是一些混合的結果。我有類似的東西編碼,但仍然得到相同的結果,當推到csv。 – user3067193