Piping foreach to html

Tagged: ,

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Noah Sparks Noah Sparks 2 years, 6 months ago.

  • Author
    Posts
  • #15795
    Profile photo of バーナード B
    バーナード B
    Participant

    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 " 
    

    Server Uptime Report

    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!!!

  • #15796
    Profile photo of Noah Sparks
    Noah Sparks
    Participant

    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

  • #15799
    Profile photo of Noah Sparks
    Noah Sparks
    Participant

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

  • #15798
    Profile photo of バーナード B
    バーナード B
    Participant

    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 " 
    

    Server Uptime Report

    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!!!

You must be logged in to reply to this topic.