2012-08-14 53 views
2

我有脚本在特定目录中监视文件创建。 我使用注册-ObjectEvent创建System.IO.FileSystemWatcher后,Powershell调试事件 - 行为代码块

它的伟大工程,但如果我在清议代码块设置一个断点的IDE生成:

警告:断点在'D:\我的东西\桌面\脚本\ fileWatcher.ps1:15'上的线断点将不会被击中

这个消息发生后,我把文件放入我正在看的目录,我可以看到我的写 - 在上述“警告”之后立即打印出我的信息。

这是正常的吗?
我需要添加更多的代码,并可以真正使用调试器。 我应该怎么做,所以我可以调试这个代码块?

$fsw = [System.IO.FileSystemWatcher] $path 

Register-ObjectEvent -InputObject $fsw –EventName Created -SourceIdentifier DDFileCreated -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
    Out-File -FilePath $logFile -Append -InputObject "The file '$name' was $changeType at $timeStamp" 
} 

回答

2

如果来自同一ISE或控制台会话直接PowerShell命令触发事件,它的实际工作:

$path = (Resolve-Path '~\').Path 
if(Test-Path "$path\newfile.txt"){ del "$path\newfile.txt" } 

$fsw = [System.IO.FileSystemWatcher] $path 

Register-ObjectEvent -InputObject $fsw –EventName Created -SourceIdentifier DDFileCreated -Action { 
    $name = $Event.SourceEventArgs.Name # put breakpoint here, via ISE or script 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
} 

'foo' | out-file "$path\newfile.txt" # breakpoint hit 

但是,如果你简单地挂上事件,并等待一些外部进程创建一个文件,我看到你遇到的问题。

所以,如果你可以添加一些测试代码来直接触发你的事件,那就去吧。

如果没有,请继续阅读...

好像你不能进入调试器,而壳或ISE是等待新命令,则可以在另一个命令执行只能进入

与理论去,这是解决方法(为我的作品):

  1. 设置断点
  2. 运行你的事件,申购代码为
  3. 运行以下命令:“读 - 主机”在ISE控制台窗口,或在shell提示
  4. 等待,直到你知道你的情况应该解雇由于外部进程
  5. 点击“进入”,让读主机来完成

中提琴,断点命中!

+0

谢谢,至少我现在知道它不是我。我用旧时尚调试器(Write-Host的船载)调试它。 – Pablo 2012-08-17 00:09:29