Sunday, December 27, 2015

Retrieve log files from a specific date

Hi,

Below is a very basic script that simply retrieve log files from a specific date,
Based on the LastWriteTime attribute of the file.

In the script i'm mapping the destination machine drive, this is because i need to use different credentials to access the remote machine.

In case you have already permission to access the remote machine C$ share / other, adjust the script below, just to save time and issue using map drive.

Write-Host "Enter The Requested Date To Filter Files,`nUse The Following Example - 24/12:" -ForegroundColor Green
$RequestedDate = Read-Host
Write-Host "Enter Computer Name / IP, To Retreive Files From..." -ForegroundColor Green
$ComputerName = Read-Host

# Map Network Drive
net use O: \\$ComputerName\C$ /USER:BlaBla BlaBla

# Check If Local Temp Path Exists
if (!(Test-Path C:\Logs\$ComputerName)) {mkdir C:\Logs\$ComputerName}

$ClientLogs = Get-ChildItem O:\LocalApp\Client\Logs\ -Recurse
$ServerLogs =  Get-ChildItem O:\LocalApp\Server\Logs\


$ClientLogs_Results = $ClientLogs | Where-Object {$_.LastWriteTime.ToString("dd/MM") -eq $RequestedDate}
$ServerLogs_Results = $ServerLogs | Where-Object {$_.LastWriteTime.ToString("dd/MM") -eq $RequestedDate}

    foreach ($ServerLogs_R in $ServerLogs_Results) {

        Write-Host -ForegroundColor Green "Copying $ServerLogs_R"
        Copy-Item -LiteralPath $ServerLogs_R.fullname -Destination C:\Logs\$ComputerName -Force
        
    } # END of ServerLogs_Results Loop

    foreach ($ClientLogs_R in $ClientLogs_Results) {

        Write-Host -ForegroundColor Green "Copying $ClientLogs_R"
        Copy-Item -LiteralPath $ClientLogs_R.fullname -Destination C:\Logs\$ComputerName -Force
        
    } # END of ServerLogs_Results Loop

# Remove Map Network Drive
Net use O: /delete /y

Write-Host "`n`nPress Enter To Exit..." -ForegroundColor Green
Read-Host



Enjoy!