0

我正在尝试编写一个 powershell 脚本,该脚本将查看文件直到它被修改,然后通过电子邮件发送发生的更改。到目前为止,我有此代码 + 将使用Net.Mail.SmtpClient发送电子邮件的代码

$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\MatchedLog"
if(!(Test-Path -Path $TARGETDIR )){
    New-Item -ItemType directory -Path $TARGETDIR
}
$FILE = 'matched.txt'

$FSW = New-Object IO.FileSystemWatcher $TARGETDIR, $FILE - Property@{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FirstName, LastWrite'}

Register-ObjectEvent $FSW Changed -SourceIdentifier FileChanged -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
}

我得到的错误是:

 Register-ObjectEvent : Cannot bind argument to parameter 'InputObject' because it is null.

我不确定为什么会这样

4

2 回答 2

1

这看起来可能是一个错字,但[IO.NotifyFilters]没有定义FirstName。你大概是说FileName

http://msdn.microsoft.com/en-us/library/system.io.notifyfilters.aspx

于 2013-06-26T20:45:40.083 回答
0

这很好用,它出于某种原因连续两次打印更改。我在网上看到很多人遇到同样的问题,但我仍在努力寻找问题所在。我还需要获取从“matched.txt”更改的行并打印它而不是“更改:/path/to/file/matched.txt”

$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\MatchedLog"
if(!(Test-Path -Path $TARGETDIR )){
    New-Item -ItemType directory -Path $TARGETDIR
}
$FILE = 'matched.txt'

$FSW = New-Object System.IO.FileSystemWatcher
$FSW.Path = $TARGETDIR
$FSW.IncludeSubdirectories = $false

$changed = Register-ObjectEvent $FSW "Changed" -Action{
    write-host "Changed: $($eventArgs.FullPath)"
}
于 2013-06-26T20:58:29.180 回答