如何使用正确的标识列导出CSV

时间:2018-01-16 20:50:57

标签: powershell ssl ssl-certificate

这不能为我提供正确的输出。我希望它在每列中列出正确的信息                 ID = $ ID                 网站= $ WebsiteURL                 有效= $ ValidTo                 Days_valid = $ ValidDays

# Script: SSL_Epire_Check_Loop.ps1
# 
$WebsiteURLs= @("www.gcincentives.com", "www.google.com" )
$WebsitePort=443
$Threshold=120
$Severe=30
$ID=0
Write-Host "#   Website_URL:    Current Certificate:    Expiration Date:    Days Remaining: Errors:"
foreach ($WebsiteURL in $WebsiteURLs){
$CommonName=$WebsiteURL
$ID+=1
Try{
$Conn = New-Object System.Net.Sockets.TcpClient($WebsiteURL,$WebsitePort) 
Try {
$Stream = New-Object System.Net.Security.SslStream($Conn.GetStream(),$false, {
param($sender, $certificate, $chain, $sslPolicyErrors) 
return $true
})
$Stream.AuthenticateAsClient($CommonName) 

$Cert = $Stream.Get_RemoteCertificate()
$CN=(($cert.Subject -split "=")[1] -split ",")[0]
$ValidTo = [datetime]::Parse($Cert.GetExpirationDatestring())

$ValidDays = $($ValidTo - [datetime]::Now).Days
$MyFontColor="darkgreen"
if ($ValidDays -lt $Threshold) {
$MyFontColor="darkyellow"
} 
if ($ValidDays -lt $Severe) {
$MyFontColor="red"
}



#Write-Host "$ID    $WebsiteURL $CN $ValidTo    $ValidDays" -ForegroundColor $MyFontColor
}
Catch { Throw $_ }
Finally { $Conn.close() }
}
Catch {
Write-Host "$ID $WebsiteURL " $_.exception.innerexception.message -ForegroundColor red
}

}
$webcert = @{ 
            ID = $ID
            Website = $WebsiteURL
            Valid = $ValidTo
            Days_valid = $ValidDays
            }

$webcert | export-csv -Path c:\users\rwhite\test1.csv

1 个答案:

答案 0 :(得分:2)

Export-Csv需要一个(自定义)对象列表,而不是哈希表。

New-Object -Type PSObject -Property $webcert |
    Export-Csv 'C:\path\to\output.csv' -NoType