Piping foreach to html

Hi,

Need some help piping a foreach result.

This is my code so far:


$computerlist = "H:\SrvCheck\servers.txt"
$output = "H:\SrvCheck\uptime.html"
$computers = Get-Content $computerlist

foreach ($computer in $computers)
{
	$Computerobj = "" | select ComputerName, Uptime, LastReboot
	$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
	$now = Get-Date
	$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
	$uptime = $now - $boottime
	$d =$uptime.days
	$h =$uptime.hours
	$m =$uptime.Minutes
	$s = $uptime.Seconds
	$Computerobj.ComputerName = $computer
	$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
	$Computerobj.LastReboot = $boottime
	$Computerobj
}
ConvertTo-Html -As Table -body " 
<h1>Server Uptime Report</h1> 
The following report was run on $(get-date)" >> $output

Invoke-Item $output 

I’m aware that I can’t have a empty pipe, but can’t remember how to do something as simple as this.

Thank you!!!

Well, it’s not clear what your end goal here is, but you would pipe the object inside of the foreach loop

$Computerobj | ConvertToHTML

If you were trying to just make one report your best bet would be to add each $Computerobj to an array, then once the foreach loop exits, convert the array to html and output it to your file.

#$computerlist = “H:\SrvCheck\servers.txt”
#$output = “H:\SrvCheck\uptime.html”
$computers = ‘localhost’,‘localhost’ #Get-Content $computerlist

$FinalObject = @()
foreach ($computer in $computers)
{
$Computerobj = “” | select ComputerName, Uptime, LastReboot
$wmi = Get-WmiObject -ComputerName $computer -Query “SELECT LastBootUpTime FROM Win32_OperatingSystem”
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
$Computerobj.ComputerName = $computer
$Computerobj.Uptime = “$d Days $h Hours $m Min $s Sec”
$Computerobj.LastReboot = $boottime
$Computerobj
$FinalObject += $Computerobj
}

$FinalObject | ConvertTo-Html

Hi Noah,
Thanks for your reply.

In the mean time, I tried this:


$computerlist = "H:\SrvCheck\servers.txt"
$DT = Get-Date -Format "yyyyMMdd-HHmm"
$output = "H:\SrvCheck\uptime_$DT.html"
$computers = Get-Content $computerlist
$Report = foreach ($computer in $computers)
{
	$Computerobj = "" | select ComputerName, Uptime, LastReboot
	$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
	$now = Get-Date
	$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
	$uptime = $now - $boottime
	$d =$uptime.days
	$h =$uptime.hours
	$m =$uptime.Minutes
	$s = $uptime.Seconds
	$Computerobj.ComputerName = $computer
	$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
	$Computerobj.LastReboot = $boottime
	$Computerobj
}
$Report |
ConvertTo-Html -As Table -body " 
<h1>Server Uptime Report</h1> 
The following report was run on $(get-date)" >> $output
Invoke-Item $output

…and it seems to work just fine.

I do like your method also.

Thanks!!!

Cool, glad you were able to get it worked out.