HTML到CSV文件转换

时间:2018-05-31 06:10:40

标签: powershell batch-file teradata

我有下面的PowerShell脚本,它显示远程服务器CPU /磁盘/内存运行状况检查。

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

电流输出: enter image description here

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

  1. 请帮我以CSV文件格式输出。
  2. 我们如何将CSV文件中的数据加载到Teradata,这可以使用PowerShell或批处理来完成吗?
  3. 代码:

    $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)
    

3 个答案:

答案 0 :(得分:3)

由于$result已经在csv中包含了您想要的信息,因此可以将其传送到Export-Csv

只需在脚本末尾添加此行:

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

未使用Teradata,但已经有关于csv import的问题:How to load data from a csv file into a table using Teradata Studio

答案 1 :(得分:2)

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

$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","",""

enter image description here

## Q:\Test\2018\05\31\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)

答案 2 :(得分:0)

$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
相关问题