Powershell:在输出中组合变量

时间:2014-10-07 22:30:21

标签: powershell

我试图整理一个非常简单的代码,输出服务器名称,上次重启日期,然后是小时和天数的差异。我已经尝试了几次Write-Host迭代,但我似乎无法获得我期望的输出。输出应该是这样的:
ServerName |重启日期|运行时间(日,小时)

以下是代码:

begin {}
process {
  foreach($server in (gc d:\win_IP.txt)){
    if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
      $strFQDN = [System.Net.Dns]::GetHostbyAddress($server) | Select-Object HostName -ErrorAction "SilentlyContinue"
      $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
      $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
      $strDiff = [DateTime]::Now - $wmi.ConvertToDateTime($wmi.LastBootUpTime) | Format-Table Days, Hours
    } else {
      Write-Verbose "$server is offline"
    }            
  }
}            

end {}

如果有人能够解释组合变量如何工作,以及如何格式化输出,我真的很感激。

提前致谢。

2 个答案:

答案 0 :(得分:6)

试试这个:

foreach ($server in (Get-Content "d:\win_IP.txt")){
    if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
        $strFQDN = [System.Net.Dns]::GetHostbyAddress($server)
        $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
        $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
        $strDiff = [DateTime]::Now - $strDate

        [PSCustomObject]@{
            "ServerName" = $strFQDN.HostName
            "Reboot Date" = $strDate
            "Time Running" = "$($strDiff.Days) days, $($strDiff.Hours) hours"
        }

    } else {
        Write-Verbose "$server is offline"
    }            
}

我所做的是将每个字段存储在一个对象中,然后输出该对象而不进行格式化。格式化对象通常不是一个好主意,因为它们会转换为字符串,除了输出到主机/文件之外不能用于任何其他内容。

答案 1 :(得分:2)

如果您使用的是PowerShell 3.0,那么Entbark的答案可能就是您所追求的。 HashTable-to-Object表示法在PowerShell 2.0中不起作用。

相反,请参阅下面的更多选项。但重点是,您可以通过创建新对象并将变量作为属性添加到该对象来组合变量。

(另外,我不知道Test-Connection,它只是我还是超级慢?)

<强>说明:

ForEach ($server in (gc 'D:\win_IP.txt')) {
    $Pinger = New-Object System.Net.NetworkInformation.Ping
    if( $Pinger.Send( $server, 500 ).Status -eq "Success" ) {
        $strFQDN = [System.Net.Dns]::GetHostbyAddress($server) |
            Select HostName -ErrorAction "SilentlyContinue"
        $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
        $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
        $strDiff = [DateTime]::Now - $strDate

合并变量选项1:

        $OutObject = New-Object PSObject
        $OutObject | Add-Member NoteProperty 'FQDN' ($strFQDN)
        $OutObject | Add-Member NoteProperty 'Date' ($strDate)
        $OutObject | Add-Member NoteProperty 'Diff' ($strDiff)

        # 1 A:
        Write-Output $OutObject

        # 1 B:
        $OutObject | Format-Table  

合并变量选项2:

        Write-Output ( (New-Object PSObject) |
            Add-Member NoteProperty 'FQDN' ($strFQDN) -PassThru |
            Add-Member NoteProperty 'Date' ($strDate) -PassThru |
            Add-Member NoteProperty 'Diff' ($strDiff) -PassThru )

合并变量选项2B:

        ( (New-Object PSObject) |
            Add-Member NoteProperty 'FQDN' ($strFQDN) -PassThru |
            Add-Member NoteProperty 'Date' ($strDate) -PassThru |
            Add-Member NoteProperty 'Diff' ($strDiff) -PassThru ) | Format-Table

<强>结尾:

    } else {
        Write-Verbose "$server is offline"
    }
}