如何将IF Else输出重定向到PowerShell中的html文件

时间:2017-03-22 03:42:58

标签: powershell

我编写了代码来获取Windows服务器的超级状态。我无法将输出重定向到html文件。你能帮忙吗?

$vCores = Get-WmiObject Win32_Processor | Measure -Property  NumberOfCores -Sum
$vCores = $vCores.Sum
$vLogicalCPUs = Get-WmiObject Win32_Processor | Measure -Property  NumberOfLogicalProcessors -Sum
$vLogicalCPUs = $vLogicalCPUs.sum
    if ($vLogicalCPUs -gt $vCores) { 
       “Hyperthreading: Enabled”
    } 
   else {  “Hyperthreading: Disabled”
    }

2 个答案:

答案 0 :(得分:0)

首先,我创建一个名为$HyperThreading的空数组。然后使用New-Object创建自定义属性值。使用Add-Member cmdlet添加标头。从这里我们将其转换为HTML并将其发送到文件。

$vCores = Get-WmiObject Win32_Processor | Measure -Property  NumberOfCores -Sum
$vCores = $vCores.Sum
$vLogicalCPUs = Get-WmiObject Win32_Processor | Measure -Property  NumberOfLogicalProcessors -Sum
$vLogicalCPUs = $vLogicalCPUs.sum
$HyperThreading = @()
    if ($vLogicalCPUs -gt $vCores) { 
       $HT=“Hyper Threading: Enabled”
    } 
   else {  $HT=“Hyper Threading: Disabled”
    }
    $HThreading = New-Object psobject
    $HThreading | Add-Member -membertype noteproperty -Name "Hyper_Threading" -Value "$HT" -Force
    $HyperThreading += $HThreading
    $HyperThreading | ConvertTo-Html -title "Hyper Threading" -body "<H2>Hyper Threading Information.</H2>" | 
    Set-Content F:\Test\Hyper_Threading.html

如果您有任何问题,请与我们联系。

答案 1 :(得分:0)

下一步评论代码段可以提供帮助

$vCores = Get-WmiObject Win32_Processor | Measure -Property  NumberOfCores -Sum
$vCores = $vCores.Sum
$vLogicalCPUs = Get-WmiObject Win32_Processor | 
                Measure -Property  NumberOfLogicalProcessors -Sum
$vLogicalCPUs = $vLogicalCPUs.sum

    if ($vLogicalCPUs -gt $vCores) { 
       $aux = 'Hyperthreading: Enabled'
    } 
    else {
       $aux = 'Hyperthreading: Disabled'
    }

'' # linebreak for better readibility
$aux | ConvertFrom-Csv -Delimiter ':' -Header "attribute","attrValue" |
       ConvertTo-Html -As List -Fragment

'<BR>' # linebreak in HTML for better readibility
$aux.Split(": ",[System.StringSplitOptions]::RemoveEmptyEntries) |
       ConvertFrom-Csv |
       ConvertTo-Html -Property Hyperthreading -As List -Fragment

<强>输出

<table>
<tr><td>attribute:</td><td>Hyperthreading</td></tr>
<tr><td>attrValue:</td><td>Disabled</td></tr>
</table>
<BR>
<table>
<tr><td>Hyperthreading:</td><td>Disabled</td></tr>
</table>

呈现的结果:

属性:超线程 attrValue:禁用


超线程:禁用