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

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

如果您知道如何在 WMI 中创建永久事件,则 PowerEvents 模块不是必需的。有关这方面的更多信息,请通过 PowerShell 查看我关于 WQL 的电子书:http ://www.ravichaganti.com/blog/?page_id=2134

于 2012-09-07T14:47:16.600 回答