【问题标题】:Array gives no output outside Function in PowerShell数组在PowerShell中的函数之外没有输出
【发布时间】:2020-03-25 13:48:33
【问题描述】:

这里是代码

$conn =  'saruspc01', '194.195.5.65'

 function lll {
    foreach($co in $conn)
    {
       $check = Test-Connection $co -Count 3 -ErrorAction SilentlyContinue
       $zugriffzeit = $check | select ResponseTime | Measure-Object ResponseTime -Average
       $avg = [system.math]::Round($zugriffzeit.Average)

        if($check -eq $null)
        {
            $pcre = Write-Output $co
            $pire = Write-Output 'False'
            $zure = 'Null'
        }
        else
        {
            $pcre = Write-Output $co
            $pire = Write-Output 'True'
            $zure = Write-Output "$avg ms"
            $zure = $zure.Replace(' ','')
        }
        $table += @( @{PCName=$pcre;    PingResult=$pire;    Zugriffszeit=$zure} )
        #$table | ForEach {[PSCustomObject]$_} | Format-Table -AutoSize
    }
}

如果我在没有 Function 的情况下执行相同的代码,它可以工作。我需要Funktion,但我想这样做。我该如何解决?

【问题讨论】:

  • 您对该函数有什么期望?你想如何调用它,预期的输出是什么?

标签: arrays powershell administrator


【解决方案1】:

最大的问题是您正在向$table 添加项目,但您没有将return 表输出返回给用户。因此,首先,将函数的结尾修改为如下所示。 (我添加了评论 #EndOfForEach 以帮助突出需要更改的地方。

        $table += @( @{PCName=$pcre;    PingResult=$pire;    Zugriffszeit=$zure} )                
    }#EndOfForEach
    return $table
}

完成该更改后,您的函数将起作用,但只有当您像这样调用它时:

PS:\>lll
Name                           Value                                             
----                           -----                                             
PCName                         behemoth                                          
Zugriffszeit                   1ms                                               
PingResult                     True                                              
PCName                         notonline                                         
Zugriffszeit                   Null                                              
PingResult                     False                                             

在这一点上,我建议进行两项更改。一,布局难以阅读,所以我将函数转换为使用 [psCustomObject] 数据类型,就像这样。

function lll {
    $results = New-Object System.Collections.ArrayList
    #...

   $results.Add([psCustomObject]@{PCName=$pcre;    PingResult=$pire;    Zugriffszeit=$zure})| Out-Null
    }#EndOfForEach
    return $results
}

对于这样的输出:

PS>lll

PCName    PingResult Zugriffszeit
------    ---------- ------------
behemoth  True       1ms         
notonline False      Null  

如果你想把你的函数提升到一个新的水平,你会很好地考虑修改函数以接受参数,而不是依赖已经设置的$conn 变量。希望这会有所帮助,如果您有任何问题或者我是否可以更深入地研究某个领域,请告诉我。

为什么$results 不维护一个值?

PowerShell 使用变量作用域,因此我们在此函数中定义的$results 变量将只存在只要函数正在运行。要保留结果,请在调用函数时将它们分配给变量,如下所示。

$MyPingResults = lll
#no output is written to screen.

PS>$MyPingResults #get all results
PCName    PingResult Zugriffszeit
------    ---------- ------------
behemoth  True       1ms         
notonline False      Null     

PS>$myPingResults[0] #get first result

PCName   PingResult Zugriffszeit
------   ---------- ------------
behemoth True       7ms         

PS>$myPingResults | Where PingResult -eq $false #only get failed results

PCName    PingResult Zugriffszeit
------    ---------- ------------
notonline False      Null        

完成的脚本

function lll {
    $results = New-Object System.Collections.ArrayList
    foreach($co in $conn)
    {
       $check = Test-Connection $co -Count 3 -ErrorAction SilentlyContinue
       $zugriffzeit = $check | select ResponseTime | Measure-Object ResponseTime -Average
       $avg = [system.math]::Round($zugriffzeit.Average)

        if($check -eq $null)
        {
            $pcre = Write-Output $co
            $pire = Write-Output 'False'
            $zure = 'Null'
        }
        else
        {
            $pcre = Write-Output $co
            $pire = Write-Output 'True'
            $zure = Write-Output "$avg ms"
            $zure = $zure.Replace(' ','')
        }
        $results.Add([psCustomObject]@{PCName=$pcre;    PingResult=$pire;    Zugriffszeit=$zure})| Out-Null
    }#EndOfForEach
    return $results
}

【讨论】:

  • 应用更改后,当我执行脚本时,它显示了结果。然后我尝试调用表数组,我在其中保存了输出,但表数组似乎是空的。只有在执行脚本时才会显示输出,否则为空
  • 我会发布完整的脚本,我想你可能错过了一个变化。
  • 啊,我看到了你的问题,我会把它编辑到我的答案中
  • 很高兴它有帮助,如果有用,请确保标记为答案并点赞:)
  • 最后一个问题。如何消除这种不需要的数组位置数: PS C:\Users\Lokal-keeran> $sd = lll #将函数 lll 放入变量 $sd PS C:\Users\Lokal-keeran> $sd 0 #wanto消除这个 1 PCName PingResult Zugriffszeit ------ ---------- ------------ keeranpc01 True 3ms 192.168.0.65 False Null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2013-12-07
相关资源
最近更新 更多