From 42cbf24b10bfbeccb2fb82795fe33eaf51d5bcc8 Mon Sep 17 00:00:00 2001 From: sawft99 <81699231+sawft99@users.noreply.github.com> Date: Wed, 7 Aug 2024 11:19:49 -0500 Subject: [PATCH] Update PingWithTime.ps1 - Rebuilt for better error handling and performance --- PingWithTime.ps1 | 80 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/PingWithTime.ps1 b/PingWithTime.ps1 index e1ef774..ef298ce 100644 --- a/PingWithTime.ps1 +++ b/PingWithTime.ps1 @@ -1,11 +1,16 @@ #Ping with time stamps #Made for PS7! -$OutFolder = $Env:USERPROFILE + '\' + 'Downloads' -$PingCount = '99999999' +#Make sure not to have a '\' at the end when passing a path +$OutFolder = 'C:\Users\Example\Downloads\Results' #$args[0] +#0 is unlimited +$PingCount = '0' # $args[1] +#Set console width. Helps prevent wraping or incomplete output +[console]::BufferWidth = 140 #-------- +$Error.Clear() Clear-Host Write-Host ' @@ -15,11 +20,18 @@ Ping test with time stamps ' function Address { - $Address = Read-Host -Prompt "Enter ip/domain to test" - if ($Address.Length -EQ 0) { - Address + $Address = $null + while ($null -eq $Address) { + try { + $Error.Clear() + $InputAddress = Read-Host -Prompt "Enter IP address to test" + $Address = [System.Net.IPAddress]::Parse($InputAddress) + } catch { + Write-Host -ForegroundColor Red 'Enter a proper IP address' + $Address = $null + } } - $Address + return $Address } function FormattedTime { @@ -28,22 +40,46 @@ function FormattedTime { $Time } -$Address = Address -$OutFile = $OutFolder + '\' + $Address + '.txt' -$TestFile = Test-Path $OutFile -if ($TestFile -eq $true) { - Write-Host '' - $LASTEXITCODE = 0 - choice /m 'File already exists. Delete first?' - Write-Host - if (($LASTEXITCODE -ne 1) -and ($LASTEXITCODE -ne 2)) { - Write-Host -ForegroundColor Red 'Error in selection - ' - Pause - Exit 1 - } elseif ($LASTEXITCODE -eq 1) { - Remove-Item $OutFile -Force +function OutFile { + $OutFile = $OutFolder + '\' + $Address + '.txt' + if (Test-Path $OutFile) { + $Error.Clear() + $DeleteChoices = @( + [System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the file"), + [System.Management.Automation.Host.ChoiceDescription]::new("&No", "Do not delete the file") + ) + $DeleteChoiceCaption = 'File already exists. Not deleting will cause the file to be appended. Delete file?' + $DeleteChoiceMessage = $Null + $Result = $Host.UI.PromptForChoice($DeleteChoiceCaption, $DeleteChoiceMessage, $DeleteChoices, -1) + Write-Host '' + if (($Result -ne 0) -and ($Result -ne 1)) { + Write-Host -ForegroundColor Red 'Error in selection + ' + exit 1 + } elseif ($Result -eq 0) { + Remove-Item $OutFile -Force + if (Test-Path $OutFile) { + Write-Host -ForegroundColor Red 'Failed to remove file' + exit 1 + } else { + Write-Host 'File deleted' + } + } + } + $OutFile +} + +function Ping { + if ($PingCount -le 0) { + Test-Connection -Ping $Address -Repeat -DontFragment -OutVariable Table | Select-Object * | Format-Table @{Name="Time";Expression={FormattedTime}},Ping,Address,Latency,Status -Expand Both -Wrap | Tee-Object -FilePath $OutFile -Append + } else { + Test-Connection -Ping $Address -Count $PingCount -DontFragment -OutVariable Table | Select-Object * | Format-Table @{Name="Time";Expression={FormattedTime}},Ping,Address,Latency,Status -Expand Both -Wrap | Tee-Object -FilePath $OutFile -Append } } -Test-Connection -Ping $Address -Count $PingCount -DontFragment -OutVariable Table | Select-Object * | Format-Table @{Name="Time";Expression={FormattedTime}},Ping,Address,Latency,Status -Wrap | Tee-Object -FilePath $OutFile -Append +#------------ + +$Address = Address +$OutFile = OutFile + +Ping