2014-10-07 34 views
1

我想把一个非常简单的代码输出服务器名称,最后一次重新启动日期,然后在几小时和几天的差异。我已经尝试了Write-Host的几次迭代,但似乎无法获得我期望的输出。输出应该是这样的:
ServerName |重新启动日期|运行时间(日,小时)Powershell:组合输出中的变量

下面是代码:

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 {} 

如果有人能解释是如何结合变量的工作原理,以及如何格式化输出,我会很感激。

在此先感谢。

+1

你得到什么输出?如果在'if'语句中使用'Test-Connection',则应该使用'-Quiet'参数。 – 2014-10-07 23:05:46

回答

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" 
    }    
} 

我所做的是店内的每个字段的一个对象,那么就输出该对象,而无需进行格式化。通常不是格式化对象的好主意,因为它们被转换为字符串,除了输出到主机/文件外,不能用于任何其他用途。

+0

这完美,Entbark!非常感谢你解释它是如何工作的;我一定会更好地理解我的下一个脚本任务的对象! – user2521736 2014-10-08 15:37:25

2

如果您使用的是PowerShell 3.0,Entbark的答案可能就是您要做的。 HashTable到Object的表示法在PowerShell 2.0中不起作用。

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

(另外,我不知道试连接的,只是我还是超慢?)

简介:

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" 
    } 
} 
+0

Sam,非常感谢您的帮助 - 编码时总会有十几种不同的方式获得相同的结果。我相信我会在我的下一个脚本中使用你的例子。感谢您花时间来解释和展示! – user2521736 2014-10-08 15:39:35