Fixed logfile path
This commit is contained in:
@@ -6,8 +6,9 @@ $addressDefinitions = @(
|
||||
"8.8.8.8-9" # Using range notation for cleaner definition
|
||||
)
|
||||
|
||||
# Set up logging configuration
|
||||
$logFolder = ".\Logs"
|
||||
# Set up logging configuration - Use absolute path to avoid system32 issues
|
||||
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$logFolder = Join-Path $scriptPath "LogsPing"
|
||||
$logFile = Join-Path $logFolder "network_monitor.log"
|
||||
$statLogFile = Join-Path $logFolder "statistics.log"
|
||||
$csvLogFile = Join-Path $logFolder "ping_data.csv"
|
||||
@@ -15,15 +16,32 @@ $errorLogFile = Join-Path $logFolder "errors.log"
|
||||
|
||||
# Create log directory if it doesn't exist
|
||||
if (-not (Test-Path $logFolder)) {
|
||||
New-Item -Path $logFolder -ItemType Directory | Out-Null
|
||||
try {
|
||||
New-Item -Path $logFolder -ItemType Directory -Force | Out-Null
|
||||
Write-Host "Created log directory: $logFolder" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host "Error creating log directory: $_" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
# Initialize log files with headers - Use streamwriter for lower memory footprint
|
||||
$null = if (-not (Test-Path $csvLogFile)) {
|
||||
$csvStream = [System.IO.StreamWriter]::new($csvLogFile)
|
||||
$csvStream.WriteLine("Timestamp,IPAddress,Status,ResponseTime,SuccessRate")
|
||||
$csvStream.Close()
|
||||
$csvStream.Dispose()
|
||||
try {
|
||||
if (-not (Test-Path $csvLogFile)) {
|
||||
# Ensure directory exists
|
||||
$csvDir = Split-Path -Parent $csvLogFile
|
||||
if (-not (Test-Path $csvDir)) {
|
||||
New-Item -Path $csvDir -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
$csvStream = [System.IO.StreamWriter]::new($csvLogFile)
|
||||
$csvStream.WriteLine("Timestamp,IPAddress,Status,ResponseTime,SuccessRate")
|
||||
$csvStream.Close()
|
||||
$csvStream.Dispose()
|
||||
Write-Host "Created CSV log file: $csvLogFile" -ForegroundColor Green
|
||||
}
|
||||
} catch {
|
||||
Write-Host "Error creating CSV log file: $_" -ForegroundColor Red
|
||||
}
|
||||
|
||||
# Prepare log streamwriters - more memory efficient than Out-File
|
||||
@@ -44,11 +62,22 @@ function Write-Log {
|
||||
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
||||
$logLine = "$timestamp - $Message"
|
||||
|
||||
# Use streamwriter instead of Out-File
|
||||
$stream = [System.IO.StreamWriter]::new($LogPath, $true)
|
||||
$stream.WriteLine($logLine)
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
# Use streamwriter instead of Out-File with error handling
|
||||
try {
|
||||
# Ensure directory exists
|
||||
$logDir = Split-Path -Parent $LogPath
|
||||
if (-not (Test-Path $logDir)) {
|
||||
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
$stream = [System.IO.StreamWriter]::new($LogPath, $true)
|
||||
$stream.WriteLine($logLine)
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
} catch {
|
||||
# Fall back to console output if file writing fails
|
||||
Write-Host "Failed to write to log file $LogPath. Error: $_" -ForegroundColor Red
|
||||
}
|
||||
|
||||
if (-not $NoConsole) {
|
||||
Write-Host $logLine
|
||||
@@ -174,10 +203,14 @@ function Write-PingDataLog {
|
||||
$statusText = if ($Status) { "UP" } else { "DOWN" }
|
||||
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
||||
|
||||
$stream = [System.IO.StreamWriter]::new($csvLogFile, $true)
|
||||
$stream.WriteLine("$timestamp,$Address,$statusText,$ResponseTime,$SuccessRate")
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
try {
|
||||
$stream = [System.IO.StreamWriter]::new($csvLogFile, $true)
|
||||
$stream.WriteLine("$timestamp,$Address,$statusText,$ResponseTime,$SuccessRate")
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
} catch {
|
||||
Write-Host "Error writing to CSV log file: $_" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
# Function to log statistics periodically - streamwriter based
|
||||
@@ -237,6 +270,14 @@ function Invoke-MemoryCleanup {
|
||||
[System.GC]::Collect()
|
||||
}
|
||||
|
||||
# Display script information
|
||||
Write-Host "Network Monitoring Tool" -ForegroundColor Cyan
|
||||
Write-Host "======================" -ForegroundColor Cyan
|
||||
Write-Host "Log Directory: $logFolder" -ForegroundColor Yellow
|
||||
Write-Host "Monitoring these IPs: $($addresses -join ', ')" -ForegroundColor Yellow
|
||||
Write-Host "Press Enter to continue or Ctrl+C to exit..." -ForegroundColor Green
|
||||
$null = Read-Host
|
||||
|
||||
# Main monitoring loop
|
||||
try {
|
||||
$pingInterval = 1 # seconds between pings
|
||||
|
||||
Reference in New Issue
Block a user