3

我正在自动化一个过程,并且已经为此制作了一个 powershell 脚本。现在我需要做一些事情,每次将新文件夹添加到特定位置(即删除新版本)时都会调用该脚本。我应该为此使用什么。WCF 太多了吗?如果没有,有任何线索吗?任何有用的链接。或者另一个powershell脚本更好?

请记住,我也需要检查子文件夹。

谢谢。

4

2 回答 2

5

个人我会使用 System.IO.FileSystemWatcher

$folder = 'c:\myfoldertowatch'
$filter = '*.*'                             
$fsw = New-Object IO.FileSystemWatcher $folder, $filter 
$fsw.IncludeSubdirectories = $true              
$fsw.NotifyFilter = [IO.NotifyFilters]'DirectoryName' # just notify directory name events
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {  ... do my stuff here } # and only when is created

使用它来停止观看事件

Unregister-Event -SourceIdentifier FileCreated
于 2012-05-10T08:03:58.243 回答
0

试试这个:

$fsw = New-Object System.IO.FileSystemWatcher -Property @{
    Path = "d:\temp"
    IncludeSubdirectories = $true #monitor subdirectories within the specified path
}

$event = Register-ObjectEvent -InputObject $fsw –EventName Created -SourceIdentifier fsw -Action {

    #test if the created object is a directory
    if(Test-Path -Path $EventArgs.FullPath -PathType Container)
    {
        Write-Host "New directory created: $($EventArgs.FullPath)"  
        # run your code/script here
    }
}
于 2012-05-10T08:19:32.150 回答