如何同时写入-host和html

时间:2013-07-17 14:28:22

标签: powershell powershell-v2.0 powershell-v3.0 powershell-ise

我已经编写了一个代码片段,并且我已经在其中合并了写主机,我想要写入主机和转换html来运行此代码段,任何人都可以帮助我。

 $arrComputers = get-Content -Path "C:\Computers.txt"
    foreach ($strComputer in $arrComputers)
    {
        $colItems = get-wmiobject -class "Win32_BIOS" -namespace "root\CIMV2" `
        -computername $strComputer
       foreach ($objItem in $colItems)
       {
         write-host "Computer Name: " $strComputer
          write-host "BIOS Version: " $objItem.BIOSVersion
      }

       $colItems1 = get-wmiobject -class Win32_logicaldisk -Filter "DeviceID = 'C:'" -computername $strComputer
       foreach ($objItem1 in $colItems1)
       {
        $e=$objItem1.freeSpace/1GB
         write-host "Total Space:  " $e
         }

        $colItems4 = Get-WMIObject -class Win32_PhysicalMemory  -computername $strComputer
      $colItems5=$colItems4 | Measure-Object -Property capacity -Sum 
      foreach ($objItem4 in $colItems5)
   {
      $e4=$colItems5.Sum/1GB
      write-host "Memory :  " $e4
   }


    }

1 个答案:

答案 0 :(得分:1)

您可以使用-Fragment参数。

$pcName = "Computer Name: $strComputer" | ConvertTo-HTML -Fragment;
$biosV = "BIOS Version: $objItem.BIOSVersion" | ConvertTo-HTML -Fragment;

ConvertTo-HTML -Body "$pcName $biosV" -Title "List of Computers" |  Out-File c:\listofcomputers.html

只需在具有相同内容的Write-Host之后创建变量,并在脚本末尾使用converTo-HTML。

这种方式允许您通过使用param标签甚至表格标签来设置HTML输出的样式,以获得更好的布局,然后使用干净的无菌布局。