2

我有以下 Powershell 脚本在 ISE 中运行良好,但我的意思是它运行并等待文件在处理之前创建。问题是,一旦我尝试从命令行运行它,它就会在$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action调用时退出。

我怎样才能让它无限期地从命令行运行,例如等待和处理满足目标文件夹中标准的每个新文件?

我一定遗漏了一些东西,但这是我第一次尝试使用 PowerShell,所以任何关于如何从 cmd 行获得此工作的建议将不胜感激。如果您对改进有任何建议,我也很乐意听到。

#variables
$folder = 'C:\DE\LIVE'
$filter = 'LOCK.txt'
$data_in = 'C:\DE\LIVE\DATA.txt'
$data_out = 'C:\DE\LIVE\RMIN\data.txt'
$destination = 'C:\DE\LIVE\RMIN\lock.txt'

#setup a file system watcher on the LIVE dir watching the file name and the last write
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
Write-Host "----------------------------------------------------"
Write-Host "Monitoring: '$folder'"

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath

    if(Test-Path ($data_in)){
        Write-Host "Processing: DATA.txt"    

        try {
            Get-Content $data_in | Foreach-Object { 
                $output = $output + $_.Trim() + "|"
            }
            $output = $output + "1"
        }
        catch {
            $error[0]
            Unregister-Event -SourceIdentifier FileCreated
            Write-Host "Error ocurred, monitoring stopped"
            Write-Host "----------------------------------------------------"
        }
        finally {
            Write-Host "Processing: DATA.txt finished"    
        }

        Write-Host "Writing: data.txt to RMIN"

        try{
            $stream = [System.IO.StreamWriter] $data_out
            $stream.WriteLine("ADD DAVE")
            $stream.WriteLine($output)
            $stream.close()     
        }
        catch{
            $error[0]
            Unregister-Event -SourceIdentifier FileCreated
            Write-Host "Error ocurred, monitoring stopped"
            Write-Host "----------------------------------------------------"
        }
        finally {
            Write-Host "Writing: data.txt to RMIN finished"
        }

        Write-Host "Moving: LOCK.txt file to RMIN"

        try{
            Move-Item $path -Destination $destination -Force -Verbose
        }
        catch{
            $error[0]
            Unregister-Event -SourceIdentifier FileCreated
            Write-Host "Error ocurred, monitoring stopped"
            Write-Host "----------------------------------------------------"
        }
        finally{
            Write-Host "Moving: LOCK.txt file to RMIN finished"
        }

        Write-Host "Deleting: DATA.txt"

        try{
            Remove-Item $data_in
        }
        catch{
            $error[0]
            Unregister-Event -SourceIdentifier FileCreated
            Write-Host "Error ocurred, monitoring stopped"
            Write-Host "----------------------------------------------------"
        }
        finally{
            Write-Host "Deleting: DATA.txt finished"
            Write-Host "----------------------------------------------------"
            Write-Host "Awaiting Next File..."
        }

    }

}
4

0 回答 0