Skip to content

Commit

Permalink
Update PingWithTime.ps1
Browse files Browse the repository at this point in the history
- Rebuilt for better error handling and performance
  • Loading branch information
sawft99 authored Aug 7, 2024
1 parent 03f5aac commit 42cbf24
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions PingWithTime.ps1
Original file line number Diff line number Diff line change
@@ -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 '
Expand All @@ -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 {
Expand All @@ -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

0 comments on commit 42cbf24

Please sign in to comment.