Skip to content

Commit

Permalink
Update forensics_log_pull.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveen Angali authored and Naveen Angali committed Nov 14, 2024
1 parent c976da8 commit c801df1
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions Azure-ARM/forensics_log_pull.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandText = $sqlQuery;
$SqlCmd.Connection = $SqlConnection;
$result = $SqlCmd.ExecuteScalar();
$SqlConnection.Close();
#$SqlConnection.Close();

# Function to check if the SQL Server starts with any of the specified values
if ($result -eq 'READ_ONLY') {
Expand All @@ -16,9 +16,47 @@ if ($result -eq 'READ_ONLY') {
} else {
Write-Output "Database is not read-only. Continuing script execution."
}

# Rest of the script
Write-Host "Executing the rest of the script..."
$LogQuery = "SELECT TOP (100) [Id]
,[Message]
,[Level]
,[TimeStamp]
,[Exception]
,[LogEvent]
,[AssemblyName]
,[AssemblyVersion]
,[SourceContext]
,[EnvironmentUserName]
,[MachineName]
FROM [logging].[tSystemLog]
order by id desc"
$SqlCmd.CommandText = $LogQuery;
$SqlDataReader = $SqlCmd.ExecuteReader();
$results = @()
while ($SqlDataReader.Read()) {
# Collect each row into a hashtable for easy export
$row = @{
Id = $SqlDataReader["Id"]
Message = $SqlDataReader["Message"]
Level = $SqlDataReader["Level"]
TimeStamp = $SqlDataReader["TimeStamp"]
Exception = if ($SqlDataReader["Exception"] -ne $null) { $SqlDataReader["Exception"] } else { "NULL" }
LogEvent = if ($SqlDataReader["LogEvent"] -ne $null) { $SqlDataReader["LogEvent"].ToString() } else { "NULL" }
AssemblyName = $SqlDataReader["AssemblyName"]
AssemblyVersion = $SqlDataReader["AssemblyVersion"]
SourceContext = if ($SqlDataReader["LogEvent"] -ne $null) { $SqlDataReader["SourceContext"].ToString() } else { "NULL" }
EnvironmentUserName = $SqlDataReader["EnvironmentUserName"]
MachineName = $SqlDataReader["MachineName"]
}
$results += [PSCustomObject]$row
}
$SqlDataReader.Close();
$SqlConnection.Close();


# Rest of the script
#Write-Host "Executing the rest of the script..."

# Get hostname of pod to know which pod the logs are from
$hostname = hostname
Expand Down Expand Up @@ -61,7 +99,8 @@ robocopy "c:\profisee\webportal\logfiles" "$env:TEMP\all-Logs\$logsFolder\Profis
robocopy "c:\inetpub\logs\LogFiles\W3SVC1" "$env:TEMP\all-Logs\$logsFolder\IISLogs" /E /COPYALL /DCOPY:T
netstat -anobq > $env:TEMP\all-Logs\$logsFolder\TCPLogs\netstat.txt
Get-NetTCPConnection | Group-Object -Property State, OwningProcess | Select -Property Count, Name, @{Name="ProcessName";Expression={(Get-Process -PID ($_.Name.Split(',')[-1].Trim(' '))).Name}}, Group | Sort Count -Descending | out-file $env:TEMP\all-Logs\$logsFolder\TCPLogs\TCPconnections.txt

$outputCsvPath = "C:\fileshare\alllogs\$logsFolder.csv"
$results | Select-Object Id, Message, Level, TimeStamp, Exception, LogEvent, AssemblyName, AssemblyVersion, SourceContext, EnvironmentUserName, MachineName | Export-Csv -Path $outputCsvPath -NoTypeInformation -Encoding UTF8
# Compress and copy to fileshare
compress-archive -Path "$env:TEMP\all-Logs\$logsFolder\" -DestinationPath "$env:TEMP\$WebAppName-$hostname-All-Logs-$DT.zip"
copy "$env:TEMP\$WebAppName-$hostname-All-Logs-$DT.zip" "C:\fileshare\alllogs\"
Expand Down

0 comments on commit c801df1

Please sign in to comment.