Testing-Connection with a loop (Function: Test-ConnectionLoop): Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) K (Admin verschob die Seite Testing-Connection with a loop nach Testing-Connection with a loop (Function: Test-ConnectionLoop, ohne dabei eine Weiterleitung anzulegen) |
Admin (Diskussion | Beiträge) |
||
(3 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
+ | <span style="font-size:20px;"><span style="color:red">'''-> This function/script is moved to [https://github.com/R-Studio/PSTools GitHub]!'''</span></span><br> | ||
+ | |||
<source lang="powershell"> | <source lang="powershell"> | ||
Function Test-ConnectionLoop { | Function Test-ConnectionLoop { | ||
Zeile 6: | Zeile 8: | ||
[Parameter(Position=0,mandatory=$true)] | [Parameter(Position=0,mandatory=$true)] | ||
[array] $Destinations, | [array] $Destinations, | ||
− | + | ||
[Parameter(Position=1,mandatory=$false)] | [Parameter(Position=1,mandatory=$false)] | ||
[int] $Port, | [int] $Port, | ||
− | + | ||
[Parameter(Position=2,mandatory=$false)] | [Parameter(Position=2,mandatory=$false)] | ||
[string] $Logfile, | [string] $Logfile, | ||
− | + | ||
[Parameter(Position=3,mandatory=$true)] | [Parameter(Position=3,mandatory=$true)] | ||
− | [int] $ | + | [int] $Intervall, |
− | [Parameter(Position= | + | [Parameter(Position=4,mandatory=$true)] |
+ | [string] $IntervallUnit, | ||
+ | |||
+ | [Parameter(Position=5,mandatory=$true)] | ||
[int] $DurationInDays | [int] $DurationInDays | ||
) | ) | ||
− | + | ||
process { | process { | ||
$EndDate = (Get-Date).AddDays($DurationInDays) | $EndDate = (Get-Date).AddDays($DurationInDays) | ||
− | + | ||
while ((get-date) -le $EndDate){ | while ((get-date) -le $EndDate){ | ||
− | + | ||
foreach ($Destination in $Destinations){ | foreach ($Destination in $Destinations){ | ||
If ($Port -lt 1) { | If ($Port -lt 1) { | ||
Zeile 32: | Zeile 37: | ||
$Output = Test-NetConnection -ComputerName $Destination -Port $Port -WarningAction SilentlyContinue | $Output = Test-NetConnection -ComputerName $Destination -Port $Port -WarningAction SilentlyContinue | ||
} | } | ||
− | + | ||
if (($Output.TcpTestSucceeded -eq $false) -or ($JobContent.ResponseTime -gt 4000)) { | if (($Output.TcpTestSucceeded -eq $false) -or ($JobContent.ResponseTime -gt 4000)) { | ||
$timestamp = get-date | $timestamp = get-date | ||
Zeile 48: | Zeile 53: | ||
Write-Host "$logtext" | Write-Host "$logtext" | ||
} | } | ||
− | + | ||
− | Start-Sleep -Seconds $ | + | If ($IntervallUnit -eq "s") { |
+ | Start-Sleep -Seconds $Intervall | ||
+ | } | ||
+ | |||
+ | If ($IntervallUnit -eq "ms") { | ||
+ | Start-Sleep -Milliseconds $Intervall | ||
+ | } | ||
+ | |||
Get-Job | Remove-Job | Get-Job | Remove-Job | ||
} | } |
Aktuelle Version vom 26. Februar 2020, 10:55 Uhr
-> This function/script is moved to GitHub!
Function Test-ConnectionLoop { [CmdletBinding()] param( [Parameter(Position=0,mandatory=$true)] [array] $Destinations, [Parameter(Position=1,mandatory=$false)] [int] $Port, [Parameter(Position=2,mandatory=$false)] [string] $Logfile, [Parameter(Position=3,mandatory=$true)] [int] $Intervall, [Parameter(Position=4,mandatory=$true)] [string] $IntervallUnit, [Parameter(Position=5,mandatory=$true)] [int] $DurationInDays ) process { $EndDate = (Get-Date).AddDays($DurationInDays) while ((get-date) -le $EndDate){ foreach ($Destination in $Destinations){ If ($Port -lt 1) { $Output = Test-Connection -ComputerName $Destination -Count 1 -WarningAction SilentlyContinue -AsJob $JobContent = (Get-Job | Receive-Job) } Else { $Output = Test-NetConnection -ComputerName $Destination -Port $Port -WarningAction SilentlyContinue } if (($Output.TcpTestSucceeded -eq $false) -or ($JobContent.ResponseTime -gt 4000)) { $timestamp = get-date $logtext = "$timestamp : Connection to $Destination -> FAIL" If ($Logfile) { Add-Content -Path $logfile -Value $logtext } Write-Host "$logtext" } else { $timestamp = get-date $logtext = "$timestamp : Connection to $Destination -> SUCCESS" If ($Logfile) { Add-Content -Path $logfile -Value $logtext } Write-Host "$logtext" } If ($IntervallUnit -eq "s") { Start-Sleep -Seconds $Intervall } If ($IntervallUnit -eq "ms") { Start-Sleep -Milliseconds $Intervall } Get-Job | Remove-Job } } } }