使用IO.FileSystemWatcher时PowerShell ISE和Powershell.exe之间的差异

时间:2013-07-31 18:46:55

标签: powershell powershell-v2.0

我在ISE中运行以下Powershell脚本,但我的意思是它运行并在处理之前等待创建文件。问题是,只要我尝试从命令行运行它,就会在$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..."
        }

    }

}

0 个答案:

没有答案