我一直在从事这个小项目。
我的输出为空白,不确定如何正确调用信息。我在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)"
这是我将如何做的我思考您正在尝试做的... [[[笑]
它做什么...#region/#endregion
调用替换整个Get-Content
内容,或以其他方式将列表加载为纯文本系统名称。 $False/$True
获得Test-Connection
响应[PSCustomObject]
$Results
集合#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"