2023-09-14 11:48:43 -04:00
# This script depends on the host, port, username, and password being defined in the calling script
2023-10-05 13:17:18 -04:00
If ( -not ( $SFTPHost ) ) { Write-Error "SFTP host is not defined, this script will exit." ; Exit }
If ( -not ( $SFTPPort ) ) { Write-Error "SFTP port is not defined, this script will exit." ; Exit }
If ( -not ( $SFTPUser ) ) { Write-Error "SFTP username is not defined, this script will exit." ; Exit }
If ( -not ( $SFTPPass ) ) { Write-Error "SFTP password is not defined, this script will exit." ; Exit }
2023-09-14 11:48:43 -04:00
$ExportedEvents = " ${Env:TEMP} \ ${Env:COMPUTERNAME} _events.csv"
# Install Posh-SSH module
If ( !( Get-Module -ListAvailable -Name Posh-SSH ) ) {
2023-10-05 13:11:38 -04:00
Write-Output "Installing Posh-SSH module"
2023-09-14 11:48:43 -04:00
Install-Module -Name Posh-SSH -Force
}
# Cleanup previous runs
If ( Test-Path $ExportedEvents ) {
2023-10-05 13:11:38 -04:00
Write-Output "Deleting exported events from previous script run "" ${ExportedEvents} """
2023-09-14 11:48:43 -04:00
Try { Remove-Item $ExportedEvents -Force -ErrorAction Stop }
2023-10-05 13:11:38 -04:00
Catch { Write-Output "Cannot remove files from previous script execution. This script cannot run." ; Exit }
2023-09-14 11:48:43 -04:00
}
# Get all available log files
$Logs = Get-WinEvent -ListLog *
2023-03-20 21:47:38 -04:00
2023-09-14 11:48:43 -04:00
# Export all significant events from all available log files
ForEach ( $Log in $Logs ) {
$LogName = $Log . LogName
2023-10-05 13:11:38 -04:00
Write-Output "Exporting events from: ${LogName} "
2023-10-05 13:33:28 -04:00
Try { Get-WinEvent -LogName $LogName -FilterXPath "Event/System[Level=1 or Level=2 or Level=3]" -ErrorAction Stop | Select-Object LogName , ProviderName , Level , Id , Message | Export-Csv -Path " ${ExportedEvents} " -Append -NoTypeInformation -ErrorAction Stop }
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_ . Exception . Message }
2023-09-14 11:48:43 -04:00
}
2023-03-20 21:47:38 -04:00
2023-09-14 11:48:43 -04:00
# Deduplicate exported events
2023-10-05 13:11:38 -04:00
Write-Output "Deduplicating and sorting all exported events"
2023-09-14 11:48:43 -04:00
Try {
$TempEvents = Import-Csv $ExportedEvents -ErrorAction Stop | Sort-Object LogName , ProviderName , Id -Unique
$TempEvents | Sort-Object -Property LogName , ProviderName | Export-Csv $ExportedEvents -NoTypeInformation -ErrorAction Stop
}
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_ . Exception . Message }
2023-09-14 11:48:43 -04:00
# Upload to SFTP site
If ( Test-Path $ExportedEvents ) {
$SFTPCred = New-Object System . Management . Automation . PSCredential ( $SFTPUser , $SFTPPass )
# Wait for a random amount of time before starting the connection and uploading
Start-Sleep -Seconds $ ( Get-Random -Minimum 1 -Maximum 120 )
# Make the connection and upload the file
$SFTPSession = New-SFTPSession -ComputerName $SFTPHost -Credential $SFTPCred -AcceptKey -Port $SFTPPort
2023-10-05 13:11:38 -04:00
Write-Output "Uploading events to SFTP site"
2023-09-14 11:48:43 -04:00
Try { Set-SFTPItem -SessionId $SFTPSession . SessionId -Path $ExportedEvents -Destination . -Force }
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_ . Exception . Message }
2023-09-14 11:48:43 -04:00
Finally { Remove-SFTPSession -SessionId $SFTPSession . SessionId }
}
2023-03-20 21:47:38 -04:00
2023-09-14 11:48:43 -04:00
# Cleanup
2023-10-05 13:11:38 -04:00
Write-Output "Deleting "" ${ExportedEvents} """
2023-09-14 11:48:43 -04:00
Remove-Item $ExportedEvents -Force -ErrorAction SilentlyContinue