-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPowerShell ile birden fazla IP'ye ping atmak
142 lines (121 loc) · 3.54 KB
/
PowerShell ile birden fazla IP'ye ping atmak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName,
[Parameter(Position=1)]
[int]$ResultCount = 150
)
$PipelineItems = @($input)
if ($PipelineItems.Count)
{
$ComputerName = $PipelineItems
}
$ComputerName |
Where-Object { -not ($_ -as [ipaddress]) } |
ForEach-Object {
$null = Resolve-DnsName $_ -ErrorAction Stop
}
$UseClearHostWhenRedrawing = $false
try {
[System.Console]::SetCursorPosition(0, 0)
} catch [System.IO.IOException] {
$UseClearHostWhenRedrawing = $true
}
Clear-Host
[array]$PingData = foreach($Computer in $ComputerName)
{
@{
'Name' = $Computer
'Pinger' = New-Object -TypeName System.Net.NetworkInformation.Ping
'Results' = New-Object -TypeName System.Collections.Queue($ResultCount)
'LastResult' = @{}
}
}
foreach ($Item in $PingData)
{
for ($Filler = 0; $Filler -lt $ResultCount; $Filler++)
{
$Item.Results.Enqueue('_')
}
}
while ($true)
{
[array]$PingTasks = foreach($Item in $PingData)
{
$Item.Pinger.SendPingAsync($Item.Name)
}
try {
[Threading.Tasks.Task]::WaitAll($PingTasks)
} catch [AggregateException] {
}
0..($PingTasks.Count-1) | ForEach-Object {
$Task = $PingTasks[$_]
$ComputerData = $PingData[$_]
if ($Task.Status -ne 'RanToCompletion')
{
$ComputerData.Results.Enqueue('?')
}
else
{
$ComputerData.LastResult = $Task.Result
switch ($Task.Result.Status)
{
'Success' { $ComputerData.Results.Enqueue('.') }
'TimedOut' { $ComputerData.Results.Enqueue('x') }
}
}
}
foreach ($Item in $PingData)
{
while ($Item.Results.Count -gt $ResultCount)
{
$null = $Item.Results.DeQueue()
}
}
if ($UseClearHostWhenRedrawing)
{
Clear-Host
}
else
{
$CursorPosition = $Host.UI.RawUI.CursorPosition
$CursorPosition.X = 0
$CursorPosition.Y = 0
$Host.UI.RawUI.CursorPosition = $CursorPosition
}
foreach ($Item in $PingData)
{
Write-Host (($Item.Results -join '') + ' | ') -NoNewline
$PingText = if ($Item.LastResult.Status -eq 'Success')
{
if (1000 -le $Item.LastResult.RoundTripTime)
{
'(999+ms)'
}
else
{
'({0}ms)' -f $Item.LastResult.RoundTripTime.ToString().PadLeft(4, ' ')
}
}
else
{
'(----ms)'
}
Write-Host "$PingText | " -NoNewline
if ($Item.LastResult.Status -eq 'Success')
{
Write-Host ($Item.Name) -BackgroundColor DarkGreen
}
else
{
Write-Host ($Item.Name) -BackgroundColor DarkRed
}
}
$Delay = 1000 - ($PingData.lastresult.roundtriptime | Sort-Object | Select-Object -Last 1)
Start-Sleep -MilliSeconds $Delay
}