等到复制过程完成并继续脚本

时间:2017-06-06 08:22:13

标签: powershell

我正在观看一个文件夹中的活动,当有某些活动时,我想删除其他一些文件,并从我看过的文件夹中复制新文件和文件夹。

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\Acer\Desktop\TestSpace\main"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "C:\Users\Acer\Desktop\TestSpace\log.txt" -value $logline

                Write-Host "Deleting"
                Remove-Item C:\Users\Acer\Desktop\TestSpace\ORA\app\ -Exclude web.config -recurse -force -Confirm:$false 
                Write-Host "Deleting done"

                #Write-Host "Copy"
                $copy = (Get-Process (Copy-Item -Path C:\Users\Acer\Desktop\TestSpace\main\app -Exclude web.config -Destination C:\Users\Acer\Desktop\TestSpace\ORA\ -Recurse -verbose)) 
                $copy | Wait-Job 
                wait-process -id $copy.id
            Write-Host "Copy Done"

              }    

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action

while ($true) {sleep 5}

这很好,但是有一个问题,该过程只运行一次,之后仍在等待。

Write-Host "Copy"
                $copy = (Get-Process (Copy-Item -Path C:\Users\Acer\Desktop\TestSpace\main\app -Exclude web.config -Destination C:\Users\Acer\Desktop\TestSpace\ORA\ -Recurse -verbose)) 
                $copy | Wait-Job 
                wait-process -id $copy.id
Write-Host "Copy Done"

0 个答案:

没有答案