这没有为我提供正确的输出。我希望它在每一列中列出正确的信息 ID = $ID 网站 = $WebsiteURL 有效 = $有效至 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 

请您参考如下方法:

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

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


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!