【发布时间】:2019-01-16 18:26:22
【问题描述】:
我正在编写一个 PowerShell 脚本,每 5 分钟自动将一些文件移动到远程服务器。
日志总是嵌套在两个目录深处。我不在乎那些目录。文件夹和文件都可以(但不总是)随机命名。在将文件传送到robocopy 之前,我已经使用本地临时目录完成了去嵌套。
我还为故障排除创建了一个新的事件日志源。在我能得到这个工作之后,我会削减那些。我相信我从任务计划程序中正确运行了它(作为启动程序 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 和参数 -WindowStyle Hidden -File "C:\PathToScript\ThisIsTheScript.ps1")。
这是自动化时的运行方式。
- 由任务计划程序触发
- 根据需要创建临时日志目录
- 取消嵌套日志并将其移动到临时日志目录
- 从源中删除所有空目录
-
robocopy按 UNC 名称发送到目标共享 - 任务计划程序完成任务
文件保留在临时日志目录中,不要复制到目标服务器,并且任务计划程序永远不会完成 - 它仍然在运行。但是,即使在 robocopy 行周围使用 Start-Job 和 Wait-Job,也会创建所有故障排除日志条目。
第 5 步和第 6 步在计划时不会发生,只有在手动运行时才会发生。为什么没有复制文件,为什么在任务计划程序中运行时任务无法完成?
# 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写一个日志,这样你就可以看到它停在哪里(或者它是否首先运行)。还可以尝试将共享映射到驱动器号,而不是使用 UNC 路径。
标签: powershell scheduled-tasks robocopy