简体   繁体   English

Powershell捕获循环输出到变量

[英]Powershell capturing loop output to a variable

Hi guys im having issues getting my head around how I capture ping response to a variable if that makes sense. 嗨,大家好,我不明白如何捕捉对变量的ping响应(如果有道理)。 As i want to be able to output back to a csv with the response. 因为我希望能够输出到带有响应的csv。 Of course there is a very good chance im approaching this in totally the wrong way ! 当然,以完全错误的方式实现此目标的机会非常高!

$PingMachines=import-Csv -path C:\temp\pcs.csv -Header cn,operatingsystem,LastLogonDate

foreach ($pc in $pingmachines.cn) {
    $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$pc'" | `
    Select-Object StatusCode

    If ($PingStatus.StatusCode -eq 0){ 
        Write-Host $pc "up"
    }     
    Else {
        Write-Host $pc "down"
    }
}

In an ideal world id love to be able to save the output ie pc,pingstatus.statuscode back to a variable but im struggling with the logic and how to increment to the variable rather than just having the last object. 在理想的世界中,id喜欢能够将输出(即pc,pingstatus.statuscode)保存回一个变量,但是却在逻辑以及如何递增到变量上挣扎,而不仅仅是拥有最后一个对象。

Thanks in advance. 提前致谢。

I rewrote your code a bit; 我稍微重写了您的代码; this works for me: 这对我有用:

$machines = import-csv -path machines.csv -header ip,os,LastLogonDate

foreach ($machine in $machines)
{
    $ip = $machine.ip
    $status = gwmi win32_PingStatus -filter "Address = '$ip'"

    if ($status.StatusCode -eq 0)
    { Write-Host $ip 'up' }
    else
    { Write-Host $ip 'down' }
}

I tested it out on a file machines.csv that looks like this: 我在文件machines.csv上进行了测试,如下所示:

"127.0.0.1","linux","2012-1-1"
"192.168.1.93","minux","2012-2-10"
"192.168.1.254","xenix","2012-3-20"
"192.168.1.66","dynix","2012-4-5"

When I run it, the output looks something like this: 当我运行它时,输出看起来像这样:

PS C:\Users\dharmatech\Documents> C:\Users\dharmatech\Documents\check-machine-status.ps1
127.0.0.1 up
192.168.1.93 up
192.168.1.254 up
192.168.1.66 up

Use the Win32_PingStatus WMI object, as it already contains the data you need. 使用Win32_PingStatus WMI对象,因为它已包含所需的数据。 When you pipe gwmi result to Select-Object , you remove all but StatusCode. gwmi结果传递给Select-Object ,将除去StatusCode之外的所有内容。

Consider 考虑

$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$pc'" | Select-Object StatusCode
$PingStatus # Contains only StatusCode

Output: 输出:

StatusCode
----------
0

Whereas the WMI class contains more members: 而WMI类包含更多成员:

$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$pc'" 
$PingStatus # Contains a lot more

Output: 输出:

Source Destination IPV4Address  IPV6Address  Bytes  Time(ms)
------ ----------- -----------  -----------  -----  --------
MyPC   Server      10.0.0.1     {}           32     1

Use the pipeline with ForEach-Object instead of the foreach( in ) construct. 将管道与ForEach-Object而不是foreach(in)构造一起使用。 Using ForEach-Object will run the commands as part of the pipeline, which will allow you to capture the output as a variable. 使用ForEach-Object将在管道中运行命令,这将使您可以将输出捕获为变量。

$PingMachines=import-Csv -path C:\temp\pcs.csv -Header cn,operatingsystem,LastLogonDate

$PingMachines.cn | ForEach-Object {
  $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$_'" | `
    Select-Object StatusCode

  If ($PingStatus.StatusCode -eq 0){ 
    Write-Host $_ "up"
  }     
  Else {
      Write-Host $_ "down"
  }
}

You can think of the pipeline version's $_ automatic variable like this: 您可以想到管道版本的$ _自动变量,如下所示:

foreach( $_ in $PingMachines.cn) {
  #code that uses $_
}

Once you have a pipeline going, you'll need to output an object instead of just printing to the screen using Write-Host: 一旦有了管道,就需要输出一个对象,而不仅仅是使用Write-Host打印到屏幕上:

$PingMachines=import-Csv -path C:\temp\pcs.csv -Header cn,operatingsystem,LastLogonDate

$PingResults = $PingMachines.cn | ForEach-Object {
  $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$_'" | `
    Select-Object StatusCode,Address
    #I added the Address property above so you would have the machine name in the output object

  If ($PingStatus.StatusCode -eq 0){ 
    Write-Host $_ "up"
  }     
  Else {
      Write-Host $_ "down"
  }

  #Send the $PingStatus object out on the pipeline, which will end up in $PingResults
  Write-Output $PingStatus
}

June Blender recently posted a good article on powershell.org that covers outputting objects vs Write-Host and creating custom objects so I won't go into the full detail here. June Blender最近在powershell.org上发表了一篇不错的文章,内容涉及输出对象与Write-Host以及创建自定义对象,因此在这里我将不做详细介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM