【问题标题】:Powershell: Combining Variables in OutputPowershell:在输出中组合变量
【发布时间】:2014-10-07 22:30:21
【问题描述】:

我正在尝试编写一个非常简单的代码,该代码输出服务器名称、上次重新启动日期以及小时和天的差异。我已经尝试了几次 Write-Host 的迭代,但我似乎无法获得我期望的输出。输出应该是这样的:
服务器名 |重启日期 |运行时间(天、小时)

代码如下:

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

如果有人能解释组合变量的工作原理以及如何格式化输出,我将不胜感激。

提前致谢。

【问题讨论】:

  • 你得到了什么输出?如果您在 if 语句中使用 Test-Connection,则应使用 -Quiet 参数。

标签: powershell


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 这很好用,Entbark!非常感谢您解释它是如何工作的;我一定会在我的下一个脚本任务中更好地了解对象!
【解决方案2】:

Entbark 的答案可能就是您所追求的——如果您使用的是 PowerShell 3.0。 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"
    }
}

【讨论】:

  • Sam,非常感谢您的帮助 - 在编码时似乎总是有十几种不同的方法可以得到相同的结果。我确定我将在下一个脚本中使用您的示例。感谢您花时间解释和演示!
猜你喜欢
  • 2022-01-10
  • 2022-01-20
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2022-10-26
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多