我一直在嘗試編寫一個腳本,該腳本從文本文件中ping出一系列計算機並將輸出導出到HTML文件。將PowerShell Foreach循環導出爲HTML
使用ForEach循環和if/else語句我已經能夠獲得一個在PowerShell中顯示的工作ping腳本,但一直未能將結果導出爲html文件。
當我運行HTML文件打開腳本,但只顯示一行「這裏是$日期平的結果」
我很新的PowerShell的所以任何類型的輸入或幫助,將不勝感激!當您使用Write-Host
$ComputersAry = Get-Content -Path "C:\Script\ping.txt"
$filepath = "C:\Script\"
$date = "{0:yyy_MM_dd-HH_mm}" -f (get-date)
$file = $filepath + "Results_" + $date + ".htm"
New-Item $filepath -type directory -force -Verbose
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
<title>
LRS Ping Results
</title>
"@
Foreach ($MachineName in $ComputersAry) {
$PingStatus = Gwmi Win32_PingStatus -Filter "Address ='$MachineName'" | Select-Object StatusCode
if($PingStatus.StatusCode -eq 0){
$output = write-host "$MachineName,Ping Success!!,$Date"
} else {
$output = write-host "$MachineName,Ping FAIL, please investigate cause ASAP!!"
}
}
$pre= "Here are the ping results for $date"
$output | Select-Object Name, Status, Date | ConvertTo-HTML -Head $Header -PreContent $pre | Out-File $file
Invoke-Item $file