0

こんにちは、PowerShellPack をインストールし、FileSystem ウォッチャー モジュールを使用していますが、ファイルをスクリプトとして保護して実行すると問題が発生します。

問題は、スクリプトを実行すると実行され、フォルダーの変更が監視されますが、スクリプトが停止すると (実行の最後に到達すると)、フォルダーが監視されなくなることです。do while ループにすべてを配置しようとしましたが、うまくいかないようです。

PowerShellPack のインストール

Import-Module -Name FileSystem


$TempCopyFolder = "c:\test"
$PatchStorage = "c:\testpatch"


Start-FileSystemWatcher -File $TempCopyFolder  -Do {   
    $SearchPath = $File
    $PatchesPath = $PatchStorage
    $NewFolderFullPath = "$($eventArgs.FullPath)"
    $NewFolderName = "$($eventArgs.Name)"
    $PathToCheck = "$PatchesPath\$NewFolderName"

    #Check if it is a filde or folder 
    switch ($ObjectType) 
           {{((Test-Path $NewFolderFullPath -PathType Container) -eq $true)}{$ObjectType = 1;break}
           {((Test-Path  $NewFolderFullPath -PathType Leaf) -eq $true)}{$ObjectType = 2;break}} 

    # Its a folder so lets check if we have a folder in the $PatchesPath already
    IF($ObjectType -eq 1){
       IF(!(Test-Path -LiteralPath $PathToCheck -EA 0))
           {
            sleep -Seconds 3

            #Make a new directory where we store the patches
              New-item -Path $PatchesPath -Name $NewFolderName -ItemType directory

            #Make a folde in the folder for TC1
            $TcFolder=$NewFolderName + '_1'
            $NewPatchesPath = "$PatchesPath\$NewFolderName"

            New-item -path $NewPatchesPath -Name $TcFolder -ItemType directory

            $CopySrc = $NewFolderFullPath
            $CopyDes = "$NewPatchesPath\$TcFolder"

           }

       # There is a folder there so lets get the next number
       Else{

            $HighNumber = Get-ChildItem -Path $PathToCheck | select -Last 1

            #Core_SpanishLoginAttemptsConfiguration_Patch_03                                       

            $NewNumber = [int](Select-String -InputObject $HighNumber.Name -Pattern "(\d\d|\d)" | % { $_.Matches } | % { $_.Value } )+1
            $TcFolder= $NewFolderName + '_' + $NewNumber

            $NewPatchesPath = "$PatchesPath\$NewFolderName"

            $CopySrc = $NewFolderFullPath
            $CopyDes = "$NewPatchesPath\$TcFolder"
           }

         #Lets copy the files to their new home now that we know where every thing goes 

         $robocopy = "robocopy.exe"
         $arguments = '''' + $CopySrc + '''' +' '+ ''''+ $CopyDes + '''' + '/E'

         Invoke-Expression  -Command "$robocopy $arguments"

         Do {sleep -Seconds 1;$p = Get-Process "robo*" -ErrorAction SilentlyContinue}
             While($p -ne $null)

        #Now lets check every thing copyed 

        $RefObj = Get-ChildItem -LiteralPath $NewFolderFullPath -Recurse
        $DifObj = Get-ChildItem -LiteralPath $CopyDes -Recurse

        IF(Compare-Object -ReferenceObject $RefObj -DifferenceObject $DifObj)
           {write-host "Fail"}
        Else{# Now lets delete the source

             Remove-Item -LiteralPath $CopySrc -Force -Recurse
             }     
}} 
4

2 に答える 2

1

これには、アドオン モジュールや WMI は必要ありません。自分で設定しFileSystemWatcherてイベントを登録するだけ。確かに、もう少しコードが増えますが、少なくとも何が起こっているかはわかります。:)

$watcher = new-object System.IO.FileSystemWatcher
$watcher.Path = 'c:\logs'
$watcher.Filter = '*.log'  # whatever you need
$watcher.IncludeSubDirectories = $true  # if needed
$watcher.EnableRaisingEvents = $true

Register-ObjectEvent $watcher -EventName Changed -SourceIdentifier 'Watcher' -Action { param($sender, $eventArgs) 
   <process event here>
}

それが終わったら:

Unregister-Event -SourceIdentifier 'Watcher'
于 2012-09-07T19:00:14.773 に答える
0

これはおそらく必要なものです: WMI と PowerEvents モジュールを使用したファイル作成の監視

WMI で永続的なイベントを作成する方法を知っている場合、PowerEvents モジュールは必須ではありません。詳細については、PowerShell 経由の WQL に関する私の eBook を参照してください: http://www.ravichaganti.com/blog/?page_id=2134

于 2012-09-07T14:47:16.600 に答える