文件系统观察程序使用脚本运行时的事件

时间:2012-09-07 13:45:50

标签: powershell powershell-v2.0 filesystemwatcher

您好我已经安装了PowerShellPack并且我正在使用FileSystem观察器模块,但是当我将文件作为脚本安全并执行它时出现问题。

问题是如果你执行它运行的脚本并监视文件夹的更改但是一旦脚本停止(到执行结束),就不再监视该文件夹。 我试图将所有内容放在do while循环中,但这似乎不起作用。

PowerShellPack Install

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
             }     
}} 

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

这可能是您需要的:Monitoring file creation using WMI and PowerEvents module

如果您知道如何在WMI中创建永久事件,则

PowerEvents模块不是必需的。有关详细信息,请通过PowerShell在WQL上查看我的电子书:http://www.ravichaganti.com/blog/?page_id=2134