【问题标题】:Select-Object & empty variable选择对象和空变量
【发布时间】:2018-04-21 07:28:24
【问题描述】:

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

不过,我无法将其放入变量中以使用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 启动时间 ------------- -- --------- 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 )
     }
 }

【问题讨论】:

  • 在您的代码中,$FinalReport 未设置为任何内容,但如果您想要过滤后的输出 $FinalReport 必须存储一些内容。您可以先将 $Finalreport = 放在 ForEach 循环之前。我个人会设置 $body = $FinalReport | Select-Object 'Computer Name','Boot Time','OU' | Sort-Object 'Boot Time' | Format-Table -AutoSize 然后将其用作 Send-MailMessage cmdlet 的 -body 参数。
  • 哦,现在这变得更奇怪了,将其作为返回: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
  • @BryceMcDonald 和 OP:Don't save results of Format-Table
  • @Matt 确实有效。现在完美报道。

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


【解决方案1】:

我想你想做<strong>$FinalReport =</strong> foreach ($computer in $ComputerList){...

【讨论】:

  • 它给了我这个作为回报:@{Computer Name=pc29658; OU=TI/pc29658;开机时间=10/23/2017 10:27:06}
  • 对不起,我弄错了。您需要将$FinalReport = 放在foreach 循环前面:$FinalReport = foreach ($computer in $ComputerList){...,我已更改此答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
相关资源
最近更新 更多