Author Posts

February 7, 2018 at 9:28 pm

Hey guys,
I'm rewriting an existing script that copies the IIS logs from our various servers and pastes them to a log folder. What i can't figure out is how to get a log file of the copied files. We use robocopy for some of our file copying, but i need to sort these files by specific dates, and robocopy can't do that. So essentially i'm hoping to be able to create a log file of what was copied similar to the /log+: of robocopy.

The script i'm rewriting is going to be ran every Saturday using Task Scheduler, to copy the last week of logs from the previous Saturday through Friday.
Here's my code –

Clear
Write-Host " "
Write-Host "Copying started at: " (get-date)
# Date selection variables -
# Set to run on saturday, from the previous saturday through Friday
$today = Get-Date -Hour 0 -Minute 0 -Second 0
$firstDay = $today.AddDays(-7)
$lastDay = $today.Addseconds(-1)

#log locations
$powersp = "path to logs"

#Log variables
$powersplog = get-childitem $powersp | where-object {$_.lastwritetime -ge $firstDay  -and $_.lastwritetime -le $lastDay }

#Get POWERSP IIS Logs
get-childitem $powersp | 
    where-object {$_.lastwritetime -ge $firstDay  -and $_.lastwritetime -le $lastDay } |
    copy-item -destination "log file dump directory"

That's where I'm stuck. I've tried using these cmdlets to get a log file created but the file is blank.

Out-File "path to log dump\file.txt" 

Also

Write-Output -InputObject $powersplog | Out-File "path to log dump\file.txt" but again the file is blank.

Ideas?

February 8, 2018 at 6:45 pm

Are you saying, that the *-transcript cmdlets don't give you what you need.

    Get-Command -Name '*-Transcript' | Ft -a

    CommandType Name             Version Source                   
    ----------- ----             ------- ------                   
    Cmdlet      Start-Transcript 3.0.0.0 Microsoft.PowerShell.Host
    Cmdlet      Stop-Transcript  3.0.0.0 Microsoft.PowerShell.Host


    # Get parameters, examples, full and Online help for a cmdlet or function

    (Get-Command -Name Start-Transcript).Parameters
    Get-help -Name Start-Transcript -Examples
    Get-help -Name Start-Transcript -Full
    Get-help -Name Start-Transcript -Online


    (Get-Command -Name Stop-Transcript).Parameters
    Get-help -Name Stop-Transcript -Examples
    Get-help -Name Stop-Transcript -Full
    Get-help -Name Stop-Transcript -Online

February 8, 2018 at 7:30 pm

I hadn't tried those yet. I'll give them a shot and see if they work for what is needed.

Thanks!

February 8, 2018 at 7:43 pm

Unfortunately that doesn't do what we need. i can't pipe objects into it to capture the files copied. All it does is capture the output from the script. Pasting the output file contents here –

**********************
Windows PowerShell transcript start
Start time: 20180208133604
Username:
RunAs User:
Machine:
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe C:\Utilities\PowerShellScripts\Get-IIS-Logs-weekly.ps1
Process ID: 5464
**********************
Transcript started, output file is C:\Utilities\robocopy\robocopy_logs\Saturday_weblog_copy_test.txt

Copying started at: 2/8/2018 1:36:04 PM

POWERSP logs copied

**********************
Windows PowerShell transcript end
End time: 20180208133612
**********************

I did find a way to capture the files copied though. Tee-Object dumps the info from the copy-item cmdlet into a text file so i can see what is copied. Not ideal but the powers above will have to live with it, I think that's the closest we'll get to robocopy type log.

February 9, 2018 at 3:56 am

Yep, Tee-Object was going to be the other suggestion, outside of putting in a loop and write on each file copy pass. That last one is the most heavy handed, but sometimes, that is the limit. The other option that hit me was putting a WMI file watcher in play and letting it output when a folder received a file.