我正在尝试编写一个 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.
我不确定为什么会这样