在 PowerShell 作业中追加到数组

huangapple go评论46阅读模式
英文:

Append to array in powershell job

问题

我找到了一段代码,可以一次性对所有系统进行ping,比其他工作示例都要好。这个代码可以逐行处理一个完整的主机文件,并且可以同时ping它们。但是,我该如何将那些正常运行的主机添加到我的$online数组中呢?我尝试在true块中添加,但没有起作用。我只是想在某个地方加入$online += $pc。任何帮助将不胜感激。谢谢。

$online = @()
$pc = Get-Content C:\servers.txt 
$pc | ForEach-Object { Test-Connection -ComputerName $_ -Count 1 -AsJob } | Get-Job | Receive-Job -Wait | Select-Object @{Name='ComputerName';Expression={$_.Address}},@{Name='Reachable';Expression={if ($_.StatusCode -eq 0) { $true } else { $false }}} | Where-Object { $_.Reachable -eq $true } | ForEach-Object { $online += $_.ComputerName }

在这个修改后的代码中,我们使用Where-Object筛选出那些可达的主机,并将它们的名称添加到$online数组中。

英文:

I was able to find a piece of code that could ping all systems at once, better than any other job examples I've come across. This thing can take an entire file full of hosts, line by line, and ping them all literally at the same time. But how can I add the ones that are up to my $online array? I tried adding in the true block but it didn't work. Im simply trying to stick $online += $pc somewhere. Any help would be appreciated. Thanks.

$online = @()
$pc = Get-Content C:\servers.txt 
$pc | ForEach-Object { Test-Connection -ComputerName $_ -Count 1 -AsJob } | Get-Job | Receive-Job -Wait | Select-Object @{Name='ComputerName';Expression={$_.Address}},@{Name='Reachable';Expression={if ($_.StatusCode -eq 0) { $true } else { $false }}} | ft -AutoSize

答案1

得分: 1

以下是您要翻译的内容:

<!-- language-all: sh -->
You can store the result of your jobs and then filter by `Reachable`. I've also simplified your code a bit and added `-AutoRemove` which I consider important to dispose your jobs when done.

$result = Get-Content C:\servers.txt | ForEach-Object {
    Test-Connection -ComputerName $_ -Count 1 -AsJob
} | Receive-Job -Wait -AutoRemoveJob | ForEach-Object {
    [pscustomobject]@{
        ComputerName = $_.Address
        Reachable    = $_.StatusCode -eq 0
    }
}

$online = $result | Where-Object Reachable

# if you want just the `ComputerName` values, you can do
$online = $result | Where-Object Reachable | ForEach-Object ComputerName

# or easier, using member-access enumeration and `.Where` method
$online = $result.Where{ $_.Reachable }.ComputerName

如果您有兴趣在枚举期间对可访问和不可访问的结果进行分组,可以使用 哈希表,其中有2个 List&lt;T&gt;

$result = @{
    Online  = [System.Collections.Generic.List[object]]::new()
    Offline = [System.Collections.Generic.List[object]]::new()
}

Get-Content C:\servers.txt | ForEach-Object {
    Test-Connection -ComputerName $_ -Count 1 -AsJob
} | Receive-Job -Wait -AutoRemoveJob | ForEach-Object {
    $obj = [pscustomobject]@{
        ComputerName = $_.Address
        Reachable    = $_.StatusCode -eq 0
    }

    if($obj.Reachable) {
        return $result[&#39;Online&#39;].Add($obj)
    }
    
    $result[&#39;Offline&#39;].Add($obj)
}

$result.Online.ComputerName # => has all reachable records
英文:

<!-- language-all: sh -->
You can store the result of your jobs and then filter by Reachable. I've also simplified your code a bit and added -AutoRemove which I consider important to dispose your jobs when done.

$result = Get-Content C:\servers.txt | ForEach-Object {
    Test-Connection -ComputerName $_ -Count 1 -AsJob
} | Receive-Job -Wait -AutoRemoveJob | ForEach-Object {
    [pscustomobject]@{
        ComputerName = $_.Address
        Reachable    = $_.StatusCode -eq 0
    }
}

$online = $result | Where-Object Reachable

# if you want just the `ComputerName` values, you can do
$online = $result | Where-Object Reachable | ForEach-Object ComputerName

# or easier, using member-access enumeration and `.Where` method
$online = $result.Where{ $_.Reachable }.ComputerName

If you're interested in grouping the results between Reachable and Not Reachable during enumeration, the way to do it is with a hash table having 2 List&lt;T&gt; values.

$result = @{
    Online  = [System.Collections.Generic.List[object]]::new()
    Offline = [System.Collections.Generic.List[object]]::new()
}

Get-Content C:\servers.txt | ForEach-Object {
    Test-Connection -ComputerName $_ -Count 1 -AsJob
} | Receive-Job -Wait -AutoRemoveJob | ForEach-Object {
    $obj = [pscustomobject]@{
        ComputerName = $_.Address
        Reachable    = $_.StatusCode -eq 0
    }

    if($obj.Reachable) {
        return $result[&#39;Online&#39;].Add($obj)
    }
    
    $result[&#39;Offline&#39;].Add($obj)
}

$result.Online.ComputerName # =&gt; has all reachable records

答案2

得分: 0

I believe the issue here is the pipe ft -autosize.

尝试将管道放在if/else语句之后,如下所示:

| ForEach-Object {
    if ($_.Reachable -eq $true) {
        $online += $_.ComputerName
    }
}

然后,如果您想查看结果,您可以始终执行:

$online | ft -AutoSize

我还建议进行更好的格式化,因为一行中的所有内容不容易阅读。尝试像这样:

$online = @()
$pc = Get-Content C:\servers.txt 
$pc | ForEach-Object { 
    Test-Connection -ComputerName $_ -Count 1 -AsJob 
} | Get-Job | Receive-Job -Wait | 
Select-Object @{Name='ComputerName';Expression={$_.Address}},@{Name='Reachable';Expression={
    if ($_.StatusCode -eq 0) { 
        $true 
    } else { 
        $false 
    }
}} | ForEach-Object {
    if ($_.Reachable -eq $true) {
        $online += $_.ComputerName
    }
}
$online | ft -AutoSize
英文:

I believe the issue here is the pipe ft -autosize.

Try to pipe after the if/else statement as per below:

| ForEach-Object {
    if ($_.Reachable -eq $true) {
        $online += $_.ComputerName
    }
}

Then if you want to view the results you can always do:

$online | ft -AutoSize

I'd also suggest a better formatting as all one line isn't easy to read. Try something like this:

$online = @()
$pc = Get-Content C:\servers.txt 
$pc | ForEach-Object { 
    Test-Connection -ComputerName $_ -Count 1 -AsJob 
} | Get-Job | Receive-Job -Wait | 
Select-Object @{Name=&#39;ComputerName&#39;;Expression={$_.Address}},@{Name=&#39;Reachable&#39;;Expression={
    if ($_.StatusCode -eq 0) { 
        $true 
    } else { 
        $false 
    }
}} | ForEach-Object {
    if ($_.Reachable -eq $true) {
        $online += $_.ComputerName
    }
}
$online | ft -AutoSize

huangapple
  • 本文由 发表于 2023年2月14日 04:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定