Select-Object&空变量

时间:2017-11-08 14:45:12

标签: powershell report psobject get-wmiobject select-object

我正在尝试为我的域计算机构建启动报告,并通过邮件自动发送。

但是,我很难将其放入变量中以使用Send-MailMessage函数发送。

这是一段代码:

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 

但是我通过PowerShell交互式窗口获取它:

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

任何想法为什么会发生?

编辑:

解决方案是这些家伙提供的:

    $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 个答案:

答案 0 :(得分:0)

我认为你想做$FinalReport = foreach ($computer in $ComputerList){...