PowerShell:ConvertTo-Html不尊重字符串,而是计算字符

时间:2015-12-07 18:36:51

标签: powershell

感谢您的任何输入,我正在编写报告并尝试将结果编译为HTML以便于阅读。我试图重写一些组件以提高可读性。这个问题似乎与将数据转换为HTML(通过ConvertTo-Html)有关。

问题
如何正确地将字符串变量转换为HTML以在我的报告中编译?

代码

    # Querying specified event log for errors within the last 30 days
    $time = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddDays(-30))
    $securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName SERVERNAME -ErrorAction Stop |
    Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
    Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
    Sort-Object EventIdentifier -unique <#|
    ConvertTo-Html -Fragment -PreContent '<h2>Security Event Log</h2>'#>

if ($securityeventviewer -eq $null){
    $securityeventviewer = 'NO SECURITY ERRORS'

    }

else {
    $securityeventviewer = $securityEventViewer
    }

$securityeventviewer | ConvertTo-Html -Fragment

结果
它似乎是变量的属性,而不是实际的字符串:

<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>0</td></tr>
</table>

预期结果
没有安全错误

我看过一些看似相似的帖子,但没有完全符合这个问题。我似乎试图将字符串转换为HTML,但我不知道我应该做什么作为一种解决方法。 $ securityeventviewer变量的值为NO SECURITY ERRORS,因此我知道转换是问题发生的地方

提前感谢您的回复!

检查更新的工作代码

    # Querying specified event log for errors within the last 30 days
$time = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddDays(-30))
$securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName AUGURSERVER2k16 -ErrorAction Stop |
    Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
    Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
    Sort-Object EventIdentifier -unique

if ($securityeventviewer -eq $null){
    #If SQL instance and server don't match produce this result
    $securityeventviewer = "NO SECURITY ERRORS" |
    ForEach-Object {Add-Member -InputObject $_ -Type NoteProperty -Name Security -Value $_; $_} |
    Select Security |
    ConvertTo-Html -Fragment -PreContent '<h2>Security Event Log</h2>'
    }

else {
$securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName AUGURSERVER2k16 -ErrorAction Stop |
    Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
    Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
    Sort-Object EventIdentifier -unique |
    ConvertTo-Html -Fragment -PreContent '<h2>Security Event Log</h2>'
    }

修改

# Querying specified event log for errors within the last 30 days
$securityEventViewer = Get-EventLog -LogName Security -After ([datetime]::Today).AddDays(-30) -EntryType Error -ComputerName AUGURSERVER2k16 -ErrorAction SilentlyContinue |
Select-Object TimeGenerated, CategoryNumber, EventID, Index, EntryType, Message |
Sort-Object Index -unique

$output = $(
if($securityEventViewer -eq $null){ 
    [PSCustomObject]@{'security' = "NO SECURITY ERRORS"} 
} else { 
    $securityEventViewer 
}
) 
write-host $output

1 个答案:

答案 0 :(得分:0)

问题是ConvertTo-HTML旨在将.Net对象转换为HTML,而不是简单的字符串。

您可能需要执行以下操作:

if ($securityeventviewer -eq $null){
    $securityeventviewer = 'NO SECURITY ERRORS'
    "<p>$securityeventviewer</p>"
    }

else {
    $securityeventviewer | ConvertTo-HTML -Fragment
    }

我的HTML不强,所以<p></p>可能是错误的,但是这个想法是合理的,所以如果你没有得到任何结果,你输出自己设计的HTML片段,但如果有结果那么PowerShell会将这些结果转换为HTML表格。

编辑:好的,假设您可以使用Get-EventLog而不必进行WMIC调用来获取事件日志,那么您应该可以缩短上面的代码对此:

# Querying specified event log for errors within the last 30 days
$SecurityEventViewer = Get-EventLog -LogName Security -After ([datetime]::Today).AddDays(-30) -EntryType Error -ComputerName AUGURSERVER2k16 |
Select-Object TimeGenerated, CategoryNumber, EventID, Index, EntryType, Message |
Sort-Object Index -unique

$Output = $(
    if($securityeventviewer -eq $null){ 
        [PSCustomObject]@{'security' = "NO SECURITY ERRORS"} 
    } else { 
        $securityeventviewer 
    }
) | ConvertTo-Html -Fragment -PreContent '<h2>Security Event Log</h2>'

现在,这会将您的HTML片段保存在$Output变量中,而不是重新使用$securityeventviewer变量。它还使用内置命令Get-EventLog,并使事件日志提供程序在将结果发送到网络之前过滤结果,而不是从远程计算机的每个日志中获取所有事件,然后在事后过滤它们在PowerShell中。它应该比你的速度快得多,除非你的WMIC调用真的比内置命令快得多。如果您的WMIC查询确实更快,那么我会让WMIC过滤结果,这应该更快。