HTML 到 CSV 文件转换

HTML to CSV file conversion

我有以下显示远程服务器 CPU/Disk/Memory 健康检查的 PowerShell 脚本。

它没有以 HTML 格式提供输出,我使用 SMTP 选项将其发送到我的电子邮件 ID。一切正常。

当前输出:

除了 HTML 输出,我还希望将此输出保存在 CSV 文件中,以便我可以使用 MLOAD 将其加载到 Teradata 中。

  1. 请帮助我获取 CSV 文件中的输出。
  2. 我们如何将 CSV 文件中的数据加载到 Teradata,这可以使用 powershell 或批处理来完成吗?

代码:

$ServerListFile = "C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\list.txt"  
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue 
$Result = @() 
ForEach ($computername in $ServerList) {
    $AVGProc = Get-WmiObject -computername $computername win32_processor | 
        Measure-Object -property LoadPercentage -Average | Select Average
    $OS = gwmi -Class win32_operatingsystem -computername $computername |
        Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) * 100) / $_.TotalVisibleMemorySize) }}
    $vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |
        Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity) * 100) } }
    $vol2 = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'E:'" |
        Select-object @{Name = "E PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity) * 100) } }
    $vol3 = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'F:'" |
        Select-object @{Name = "F PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity) * 100) } }

    $result += [PSCustomObject] @{ 
        ServerName = "$computername"
        CPULoad    = "$($AVGProc.Average)%"
        MemLoad    = "$($OS.MemoryUsage)%"
        CDrive     = "$($vol.'C PercentFree')%"
        EDrive     = "$($vol2.'E PercentFree')%"
        FDrive     = "$($vol3.'F PercentFree')%"
    }

    $Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
                    <BODY background-color:peachpuff>
                    <font color =""#99000"" face=""Microsoft Tai le"">
                    <H2> Server Health Report </H2></font>
                    <Table border=1 cellpadding=0 cellspacing=0>
                    <TR bgcolor=Cyan align=center>
                    <TD><B>EP_Server_Name</B></TD>
                    <TD><B>CPU_Utilizied</B></TD>
                    <TD><B>Memory_Utilized</B></TD>
                    <TD><B>C_Drive_Free_Percentage</B></TD>
                    <TD><B>E_Drive_Free_Percentage</B></TD>
                    <TD><B>F_Drive_Free_Percentage</B></TD></TR>"

    Foreach ($Entry in $Result) { 
        if (($Entry.CpuLoad) -ge "80" -or ($Entry.memload) -ge "80" -or ($Entry.Cdrive) -le "20" -or ($Entry.Edrive) -le "20" -or ($Entry.Fdrive) -le "20") { 
            $Outputreport += "<TR bgcolor=red>" 
        } 
        else {
            $Outputreport += "<TR bgcolor=green>" 
        }
        $Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Cdrive)</TD><TD align=center>$($Entry.Edrive)</TD><TD align=center>$($Entry.Fdrive)</TD></TR>" 
    }
    $Outputreport += "</Table></BODY></HTML>" 
} 

$Outputreport | out-file C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\Test.htm 
#Invoke-Expression C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\Test.htm
##Send email functionality from below line, use it if you want   
$smtpServer = "Random"
$smtpFrom = "test@test.com"
$smtpTo = "test@test.com"
$messageSubject = "Servers Health report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\test.htm
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
$result = @()

for ($i=1; $i -lt 5; $i++) {
    $result += [PSCustomObject] @{ 
            ServerName = "$i"
            CPULoad = "$($AVGProc.Average)%"
            MemLoad = "$($OS.MemoryUsage)%"
            CDrive = "$($vol.'C PercentFree')%"
            EDrive = "$($vol2.'E PercentFree')%"
            FDrive = "$($vol3.'F PercentFree')%"
        }
}

$result | ConvertTo-Csv

因为 $result 已经在 csv 中包含了你想要的信息,所以它可以通过管道传输到 Export-Csv

只需在脚本末尾添加这一行:

$result | Export-Csv -Path C:\folder\health_check.csv -NoTypeInformation

没有使用过 Teradata 但已经有一个关于 csv 导入的问题:

您需要做的就是,附加到您的脚本中:

$Result | Export-Csv "C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\Test.csv" -NoType

或您想要存储文件的任何路径


编辑出于兴趣修改了脚本。

  • 简化路径使用
  • 已插入 function DriveFreePercent 并删除了中间(不必要的)变量。
  • 将 html 创建移到 foreach
  • 之外

示例 Csv 文件

"ServerName","CPULoad","MemLoad","CDrive","EDrive","FDrive"
"HP-G1610","4","32,61","94,56","",""

## Q:\Test18\SO_50617742.ps1
$BaseDir = "C:\Users\HOSDUM01\Desktop\Auto\Server_health_check\"
$ServerListFile = Join-Path $BaseDir "Server_list.txt"
$ReportHtm =      Join-Path $BaseDir "Server_Test.htm"
$ReportCsv =      Join-Path $BaseDir "Server_Test.csv"

function DriveFreePercent {
  param ([string]$ComputerName,
         [string]$DriveLetter)
    Get-WmiObject -Class win32_Volume -ComputerName $ComputerName -Filter "DriveLetter = '$($DriveLetter):'" |
        ForEach {"{0:N2}" -f  (($_.FreeSpace / $_.Capacity)*100) }
}

$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$Result = @()
ForEach($computername in $ServerList) {
    $Result += [PSCustomObject] @{
        ServerName = "$computername"
        CPULoad = ("{0}" -f (Get-WmiObject -ComputerName $computername win32_processor|Measure LoadPercentage -Average).Average)
        MemLoad = (gwmi -Class win32_operatingsystem -ComputerName $computername |
                   ForEach{"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/$_.TotalVisibleMemorySize)})
        CDrive  = (DriveFreePercent $ComputerName C)
        EDrive  = (DriveFreePercent $ComputerName E)
        FDrive  = (DriveFreePercent $ComputerName F)
    }
}

$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
                <BODY background-color:peachpuff>
                <font color =""#99000"" face=""Microsoft Tai le"">
                <H2> Server Health Report </H2></font>
                <Table border=1 cellpadding=0 cellspacing=0>
                <TR bgcolor=Cyan align=center>
                <TD><B>EP_Server_Name</B></TD>
                <TD><B>CPULOAD</B></TD>
                <TD><B>MemLoad</B></TD>
                <TD><B>C_Free</B></TD>
                <TD><B>E_Free</B></TD>
                <TD><B>F_Free</B></TD></TR>"

Foreach($Entry in $Result)  {
    if( ($Entry.CpuLoad) -ge "80" -or
        ($Entry.Memload) -ge "80" -or
        ($Entry.Cdrive)  -le "20" -or
        ($Entry.Edrive)  -le "20" -or
        ($Entry.Fdrive)  -le "20") {
        $Outputreport += "<TR bgcolor=red>"
    } else {
        $Outputreport += "<TR bgcolor=green>"
    }
    $Outputreport += "<TD>$($Entry.Servername)</TD>"
    $Outputreport += "<TD align=center>$($Entry.CPULoad)%</TD>"
    $Outputreport += "<TD align=center>$($Entry.MemLoad)%</TD>"
    $Outputreport += "<TD align=center>$($Entry.Cdrive)%</TD>"
    $Outputreport += "<TD align=center>$($Entry.Edrive)%</TD>"
    $Outputreport += "<TD align=center>$($Entry.Fdrive)%</TD></TR>"

    $Outputreport += "</Table></BODY></HTML>"
}

$Outputreport | Out-File $ReportHtm
$Result | Export-Csv $ReportCsv -NoTypeInformation
#Invoke-Expression $ReportHtm
#gc $ReportCsv

##Send email functionality from below line, use it if you want
$smtpServer = "Random"
$smtpFrom = "test@test.com"
$smtpTo = "test@test.com"
$messageSubject = "Servers Health report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += $Outputreport
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
#$smtp.Send($message)