Search code examples
powershellpowershell-2.0powershell-3.0powershell-remotingpowershell-ise

How to store data from foreach function in HTML file in powershell and get Physicall ram


I have written a for each file which stores the BIOS information of the systems in a network and the result is being displayed on my console but I want them to be in a HTML file in an order.

Code:

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


    }

Can you please help me in saving all the above data in HTML


Solution

  • You need to look at the ConvertTo-Html cmdlet.

    Get-WmiObject -Class Win32_BIOS -ComputerName localhost,$env:COMPUTERNAME | 
    Select PSComputerName,Version,SerialNumber | 
    ConvertTo-Html | 
    Out-File c:\test3.html
    

    Another method based on OPs update:

    $arrComputers = get-Content -Path "C:\Computers.txt"
    
    $arrComputers | ForEach-Object { Get-WMIObject -Class Win32_BIOS -ComputerName $_ } | 
    Select PSComputerName, Version, Manufacturer | 
    ConvertTo-Html | 
    Out-File C:\test4.html