用于循环的哈希表-ping设置持续时间的IP地址

问题描述 投票:0回答:1

我一直在从事这个小项目。

我的输出为空白,不确定如何正确调用信息。我在Excel中一直保持一致]

#Output Hash Table as CSV$IPAddressHash.GetEnumerator()

我还认为我在这里缺少一些信息...$IPAddressHash.Add($IPAddressToPing)

提前感谢

#Place holder
$InputText1 = #unused
$InputText2 = "TestSite" ##Popup input box


$DateTime = $(get-date -Format yyyyddmm_hhmmtt)

#Set Output File
$OutputCSVFilePath = "C:\MSP_PingLogs\ $($InputText2) $($DateTime).csv"

#Hash Table to hold results
$IPAddressHash = [ordered]@{}

#Set Information
$IPAddressToPing = "192.168.0.147"
$SecondsToRun = 5
 
#Test Ping
for ($i = 0; $i -lt $SecondsToRun; $i++){
if (Test-Connection -ComputerName $IPAddressToPing -Count 1 -Quiet){
Write-Host "Yeaaaaa, I Did IT" $($IPAddressToPing) at $(Get-Date), $responseTime -ForegroundColor Green
$IPAddressHash.Add($IPAddressToPing)
}
else {
Write-Host "FAILED PING " $($IPAddressToPing) at $(Get-Date), $responseTime -ForegroundColor Red
$IPAddressHash.Add($IPAddressToPing)
}
Start-Sleep -Seconds 1
    }



#Output Hash Table as CSV
$IPAddressHash.GetEnumerator()

#Show where file is
Write-Host "Output can be found at: $($OutputCSVFilePath)"
powershell loops for-loop hash ping
1个回答
0
投票

这是我将如何做的我思考您正在尝试做的... [[[笑]

它做什么...

    创建要ping的系统列表用#region/#endregion调用替换整个Get-Content内容,或以其他方式将列表加载为纯文本系统名称。
  • 设置各种常量
  • 通过系统列表重复
  • 通过测试计数重复
  • $False/$True获得Test-Connection响应
  • 将“可pinging的”信息写入屏幕
  • 创建具有所需属性的[PSCustomObject]
  • 将其发送到$Results集合
  • 在屏幕上显示收藏夹
  • 将集合保存到CSV文件中
  • 这是代码...

    #region >>> fake reading in a text file # in real life, use Get-Content $ComputerList = @( 'LocalHost' '10.0.0.1' '127.0.0.1' 'BetterNotBeThere' ) #endregion >>> fake reading in a text file $TestCount = 3 $DateTimeFormat = 'yyyy-MM-dd HH:mm:ss.ff' $LogDir = $env:TEMP $PingLog = 'PingLog_-_{0}.csv' -f (Get-Date).Date.ToString('yyyy-MM-dd') $FullPingLog = Join-Path -Path $LogDir -ChildPath $PingLog $Results = foreach ($CL_Item in $ComputerList) { foreach ($TC_Item in 1..$TestCount) { $Pingable = Test-Connection -ComputerName $CL_Item -Count 1 -Quiet if ($Pingable) { Write-Host ('Ping of {0} was successful.' -f $CL_Item) -ForegroundColor 'Green' } else { Write-Warning (' Unable to Ping {0} ...' -f $CL_Item) } } [PSCustomObject]@{ ComputerName = $CL_Item Pingable = $Pingable TimeStamp = Get-Date -Format $DateTimeFormat } } # show on screen $Results | # this forces things to show immediately # otherwise the "Write-*" calls might interleave with the output stream Out-Host # send to CSV $Results | Export-Csv -LiteralPath $FullPingLog -NoTypeInformation

    输出到屏幕...

    Ping of LocalHost was successful. Ping of LocalHost was successful. Ping of LocalHost was successful. WARNING: Unable to Ping 10.0.0.1 ... WARNING: Unable to Ping 10.0.0.1 ... WARNING: Unable to Ping 10.0.0.1 ... Ping of 127.0.0.1 was successful. Ping of 127.0.0.1 was successful. Ping of 127.0.0.1 was successful. WARNING: Unable to Ping BetterNotBeThere ... WARNING: Unable to Ping BetterNotBeThere ... WARNING: Unable to Ping BetterNotBeThere ... ComputerName Pingable TimeStamp ------------ -------- --------- LocalHost True 2020-04-10 19:47:33.98 10.0.0.1 False 2020-04-10 19:47:45.62 127.0.0.1 True 2020-04-10 19:47:45.65 BetterNotBeThere False 2020-04-10 19:47:45.68

    PingLog_-_2020-04-10.csv文件的内容...

    "ComputerName","Pingable","TimeStamp" "LocalHost","True","2020-04-10 19:47:33.98" "10.0.0.1","False","2020-04-10 19:47:45.62" "127.0.0.1","True","2020-04-10 19:47:45.65" "BetterNotBeThere","False","2020-04-10 19:47:45.68"

  • © www.soinside.com 2019 - 2024. All rights reserved.