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