I'm working on a PowerShell script to automatically move some files to a remote server every 5 minutes.
The logs are always nested two directories deep. I don't care about those directories. Both the folders and the files can be, but are not always, randomly named. I have used the local temp directory to accomplish the de-nesting prior to shuttling the files over to robocopy
.
I've also created a new event log source for troubleshooting. I'll cut those down after I can get this working. I believe I'm running this from Task Scheduler correctly (as start a program C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
with arguments -WindowStyle Hidden -File "C:\PathToScript\ThisIsTheScript.ps1"
).
Here's how it should go when automated.
- Triggered by Task Scheduler
- Create temp log directory if needed
- De-nest and move logs to temp log directory
- Remove all empty directories from source
robocopy
to destination share by UNC name- Task Scheduler completes task
The files remain in the temp log directory, do not copy to the destination server, and Task Scheduler never completes - it remains running. However, all of the troubleshooting log entries are created, even if using Start-Job
and Wait-Job
around the robocopy
line.
Steps 5 and 6 are not happening when scheduled, only when run manually. Why aren't the files copying, and why won't the task complete when run in Task Scheduler?
# Source Variables
$LogSource = 'C:\Somewhat\Deeper\Directory\Log\Files\Dropped\Here'
$TempLogDir = 'C:\SomeFilesBrieflyHere'
# Destination variables
$DestHost = 'thisisthedestinationcomputername'
$DestShare = 'thisisthedestinationsharename$'
# Operating Variables
$Switches = @("/s", "/MOVE", "/zb", "/R:0", "/W:0", "/MT:8", "/NFL", "/NDL", "/NP", "/IS", "/IT")
$RoboDo = @("$TempLogDir","\\$DestHost\$DestShare","$Switches")
# Verify event log source exists and create it if it does not
if (-not ([System.Diagnostics.EventLog]::SourceExists("PowerShell Script"))) {
New-EventLog -LogName 'Application' -Source 'PowerShell Script'
Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message 'Created new Event Log: Application Source type: PowerShell Script'
}
# Write log for init
Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message 'The scheduled task to move logs to the log server has been triggered and will now execute.'
# Verify Local LogDir exists
if (!(Test-Path -Path $TempLogDir)) {
New-Item -ItemType directory -Path $TempLogDir
}
# De-nest logs to local log folder in preparation to move
Get-ChildItem -Path "$LogSource" | Get-ChildItem | Get-ChildItem | Move-Item -Destination "$TempLogDir" -Force
Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message "Files locally de-nested into $TempLogDir."
# Remove all empty directories with unicorn glitter
Get-ChildItem $LogSource -Recurse | Where {$_.PSIsContainer -and @(Get-ChildItem -LiteralPath:$_.FullName).Count -eq 0} | Remove-Item -Confirm:$false -Force
Get-ChildItem $TempLogDir -Recurse | Where {$_.PSIsContainer -and @(Get-ChildItem -LiteralPath:$_.FullName).Count -eq 0} | Remove-Item -Confirm:$false -Force
Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Mes
# Begin Robocopy operation
robocopy $TempLogDir \\$DestHost\$DestShare $Switches
Write-EventLog -LogName 'Application' -Source 'Powershell Script' -EventID 1 -EntryType Information -Message "The scheduled task to move logs to the log server has been completed and the files were successfully copied to $DestHost."
robocopy
write a log, so that you can see where it stalls (or if it even runs in the first place). Also try mapping the share to a drive letter instead of using an UNC path. – Ansgar Wiechers