我正在自動化一個進程,並已爲此創建了一個PowerShell腳本。現在我需要做一些事情,每次將新文件夾添加到特定位置時都會調用該腳本,即刪除新構建。 我應該使用什麼。 WCF太多了嗎?如果沒有,爲此獲得任何線索?任何有用的鏈接。 還是另一個更好的PowerShell腳本?將新文件夾添加到某個位置時的觸發器腳本
請記住我也需要檢查子文件夾。
謝謝。
我正在自動化一個進程,並已爲此創建了一個PowerShell腳本。現在我需要做一些事情,每次將新文件夾添加到特定位置時都會調用該腳本,即刪除新構建。 我應該使用什麼。 WCF太多了嗎?如果沒有,爲此獲得任何線索?任何有用的鏈接。 還是另一個更好的PowerShell腳本?將新文件夾添加到某個位置時的觸發器腳本
請記住我也需要檢查子文件夾。
謝謝。
Personnaly I'ld使用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
試試這個:
$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
}
}
使用[IO.NotifyFilters]'DirectoryName'不需要測試是否是文件夾。 –
@Christian +1酷,注意! –
非常感謝謝伊! –
但是這個腳本應該是所有運行那個時候,因爲我永遠不知道什麼時候新的構建被丟棄。這種實現的安全性和穩定性如何? –
您可以使用IO.FileSystemWatcher類在.net(vb/c#)中創建一個應用程序(windows service也許?),它可以完成相同的工作。我個人使用生產服務器上的PowerShell腳本,從大約一年的時間做同樣的事情(排除維護重啓;)) –
經過測試。工作:D非常感謝Christian和Shay。我對此很新,這對我來說意義重大。 –