我正在尝试编写一个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.
我不确定为什么会发生这种情况
答案 0 :(得分:1)
这看起来可能是拼写错误,但[IO.NotifyFilters]
没有定义FirstName
。你可能意思是FileName
http://msdn.microsoft.com/en-us/library/system.io.notifyfilters.aspx
答案 1 :(得分:0)
这很好用,它出于某种原因连续两次打印更改。我看到很多人在网上遇到同样的问题,但我仍在努力找到问题所在。我还需要从“matched.txt”获取更改的行并打印而不是“Changed:/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)"
}