1

I'm trying to build an boot report for my domain computers and send it automatically by mail.

though, I'm having trouble to put it into a variable to send using Send-MailMessage function.

Here's the piece of code:

foreach ($computer in $ComputerList){
     If (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue -Quiet -Count 2  )
        {
        $ComInfo = (Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -Credential $Cred).LastBootUpTime
        #Convert the last boot time to a reader freindly date time format
        $BootTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($ComInfo)
        $Canonical = (Get-ADComputer $Computer -Properties CanonicalName).CanonicalName
        New-Object PSObject -Property @{            
         'Computer Name'    = $Computer                  
         'Boot Time'    =  $BootTime
         'OU'        =  $Canonical
            }
        } #end-if test
     Else
     {
        $offlines = "$Computer desligado" 
     }
 }

$FinalReport | Select-Object 'Computer Name','Boot Time','OU' |
Sort-Object 'Boot Time' | Format-Table -AutoSize 

But I'm getting it through powershell Interactive Window:

ComputerName     OU         Boot Time          
-------------    --         ---------          
PC60423          TI/PC60423 08/11/2017 11:31:38
PC60432          TI/PC60432 08/11/2017 09:26:52
PC60431          TI/PC60431 08/11/2017 08:56:53
PC60439          TI/PC60439 08/11/2017 08:42:31
PC60425          TI/PC60425 08/11/2017 10:11:43
PC60427          TI/PC60427 07/11/2017 17:07:26
PC34844          TI/PC34844 06/11/2017 15:21:06
PC42331          TI/PC42331 08/11/2017 08:10:52
PC47521          TI/PC47521 08/11/2017 07:09:01
PC36737          TI/PC36737 08/11/2017 07:47:11
PC47524          TI/PC47524 08/11/2017 08:22:24
PC30473          TI/PC30473 08/11/2017 10:06:33
PC29658          TI/PC29658 23/10/2017 10:27:06

Any ideas why is it happening?

EDIT:

solution was what these guys offered:

    $finalreport = foreach ($computer in $ComputerList)
{
     If (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue -Quiet -Count 2  )
        {
        $ComInfo = (Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -Credential $Cred).LastBootUpTime
        $BootTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($ComInfo)
        $Canonical = (Get-ADComputer $Computer -Properties CanonicalName).CanonicalName
        New-Object PSObject -Property @{            
         'Computer Name'    = $Computer 
         'OU'        =  $Canonical                 
         'Boot Time'    =  $BootTime

            }
        } #end-if test
     Else
     {
         $offlines = @($offlines + $computer )
     }
 }
  • 1
    In your code, $FinalReport isn't set to anything, but if you're wanting filtered output $FinalReport must be storing something. You could start by putting `$Finalreport = ` before your ForEach loop. Personally I'd set `$body = $FinalReport | Select-Object 'Computer Name','Boot Time','OU' | Sort-Object 'Boot Time' | Format-Table -AutoSize ` then use that as the `-body` parameter of the Send-MailMessage cmdlet. – Bryce McDonald Nov 08 '17 at 14:52
  • Oh, now this is getting even weirder, getting this as return: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData – Sidney Morais Nov 08 '17 at 18:13
  • 1
    @BryceMcDonald and OP: [Don't save results of `Format-Table`](https://stackoverflow.com/a/36358921/3829407) – Matt Nov 08 '17 at 18:19
  • @Matt it really worked. perfectly reported now. – Sidney Morais Nov 08 '17 at 18:34

1 Answers1

0

I think you want to do $FinalReport = foreach ($computer in $ComputerList){...

iRon
  • 20,463
  • 10
  • 53
  • 79
  • it got me this as return: @{Computer Name=pc29658; OU=TI/pc29658; Boot Time=10/23/2017 10:27:06} – Sidney Morais Nov 08 '17 at 16:49
  • Sorry, I was mistaken. You will need to put the `$FinalReport =` in front of the `foreach` loop: `$FinalReport = foreach ($computer in $ComputerList){...`, I have changed this n my answer. – iRon Nov 08 '17 at 19:19