我正在編寫一個powershell腳本,它將啓動一個進程,監視文件夾的文件更改,然後在添加/更改文件時備份該文件夾。Powershell腳本在ISE中工作,但不在控制檯中
如果我在Powershell ISE中運行我的腳本,它可以正常工作,我可以監視該文件夾,並且它會按預期正確保存備份。
問題是我想運行一個批處理文件來運行powershell腳本。但是,無論何時我從PowerShell控制檯運行腳本,或者運行運行腳本的批處理文件時,它都不再起作用。腳本運行並註冊事件。當我將文件複製到監視文件夾時,我只獲取已更改的事件,而不是創建的事件,並且不再調用doStuff函數。我不知道如何去調試這個:/
以下是我的腳本。我已經刪除了與當前的actualyl錯誤不相關的部分,所以我在這裏使用的一些變量不會被聲明,但它們在那裏。我得到了寫主機時改變的事件發生,但不是創建的事件(儘管如說在ISE早期的教,我得到的事件和一切工作只是罰款)控制檯
#unregister events, in case they weren't unregistered properly before. Just error siliently if they don't exist
Unregister-Event ConsoleStopped -ErrorAction SilentlyContinue
Unregister-Event FileCreated -ErrorAction SilentlyContinue
Unregister-Event FileChanged -ErrorAction SilentlyContinue
Unregister-Event TimerTick -ErrorAction SilentlyContinue
#start the console process
Write-Host Starting console process...
$consoleProcess = Start-Process "$consoleExe" -PassThru
#register to listen for when the console stops
Register-ObjectEvent $consoleProcess Exited -SourceIdentifier ConsoleStopped -Action {
Write-Host Console stopped
#unregister events
Unregister-Event ConsoleStopped -ErrorAction SilentlyContinue
Unregister-Event FileCreated -ErrorAction SilentlyContinue
Unregister-Event FileChanged -ErrorAction SilentlyContinue
if(!$timer.Enabled) {
Unregister-Event TimerElapsed -ErrorAction SilentlyContinue
Remove-Item $timer
}
Remove-Item $fsw
Remove-Item $consoleProcess
}
#watch all files/folders
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName, FileName, LastWrite'}
#register for FileCreated event
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
write-host Created event has occurred
doStuff($Event)
}
#register for FileChanged event
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
Write-Host Change event has occurred
doStuff($Event)
}
function doStuff($event)
{
write-host doStuff has been called
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' in '$folder' was $changeType at $timeStamp" -fore green
if(!$timer.Enabled) {
Write-Host Starting save timer
Register-ObjectEvent $timer Elapsed -SourceIdentifier TimerElapsed -Action $TimerAction
$timer.Start()
Out-File "$backupDir\backup.log" -Append -Force -InputObject "A request for a backup created at $timeStamp"
}
else {
Write-Host A backup has already been request
}
}
function backupSave()
{
Write-Host Starting backup...
$timestamp = Get-Date -Format o | foreach { $_ -replace ":", "." }
Copy-Item $folder "$backupDir\backup_$timestamp" -Recurse -Force
}
感謝您的建議! 我實際上已經嘗試過,但不幸的是它沒有工作:/ 實際上,我已經將doStuff移動到了開始位置,但不是backupSave,我會盡量在明天可以移動它們,作品我會回覆 – kamcknig
你還可以包含一些輸出嗎?和任何錯誤消息? – HAL9256
如果我在ISE,我會得到所有預期的輸出。當我複製一個新文件時,我得到一個創建的事件,然後幾個更改的事件(取決於文件的大小),然後是所有正確的消息以備份和備份文件。當我在powershell控制檯中運行它並通過唯一的輸出複製一個文件時,我得到的是「已更改的事件已發生」輸出,但沒有錯誤。 – kamcknig