2015-09-24 57 views
0

有一個名爲status.html的文件,它用作SharePoint服務器上WFE之間的負載均衡器。我的查詢是我想要一個腳本或一個機制,只要這個文件被某人編輯就會觸發一個郵件。 這可能嗎?檢查誰編輯了我的文件

在我的研究,我發現這個腳本:

$ACL = new-object System.Security.AccessControl.DirectorySecurity 
$AccessRule = new-object System.Security.AccessControl.FileSystemAuditRule("domain\seitconsult","Modify","success") 
$ACL.SetAuditRule($AccessRule) 
$ACL | Set-Acl "C:\windows\system32\cmd.exe" 

但我不知道這會工作。另外,如何使用此腳本觸發電子郵件?

回答

0

我看到兩種方式來實現自己的目標:

  1. 附加一個「發送電子郵件」任務到事件中的問題:

    1. 打開事件查看器(eventvwr.msc)和選擇您想要通知的事件。
    2. 點擊操作→將任務附加到此事件中…
    3. 逐步向導並選擇發送郵件動作部分。
    4. 填寫詳細信息並完成嚮導。

    請參閱here瞭解更多信息。

  2. 建立一個FileSystemWatcher

    $folder = 'C:\your\html\folder' 
    $file = 'status.html' 
    
    $from = '[email protected]' 
    $to  = '[email protected]' 
    $subject = "$file was modified" 
    
    $monitor = New-Object IO.FileSystemWatcher $folder, $file -Property @{ 
        IncludeSubdirectories = $false 
        NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' 
    } 
    
    Register-ObjectEvent $monitor Changed -SourceIdentifier FileChanged -Action { 
        $name = $Event.SourceEventArgs.FullPath 
        $reader = New-Object System.IO.StreamReader($name) 
        $msg = $reader.ReadToEnd() 
        Send-MailMessage -From $from -To $to -Subject $subject -Body $msg 
        $reader.Close() 
    } 
    

    然而,由於NotifyFilters不包括你需要描述here提取從各自的審計事件在事件日誌的用戶名。

    觀察器可以通過其源標識符被刪除:

    Unregister-Event -SourceIdentifier FileChanged 
    
+0

非常感謝您的意見。你提到的C#代碼,請你讓我知道我該如何運行它?我的意思是在我將要實現的那些沒有C#編譯器的服務器上。此外,上述的強大shell代碼會在事件查看器中生成一個事件嗎?請提供您的意見。 – Lilly123

+0

@ Lilly123您需要將C#代碼翻譯爲PowerShell(使用'Get-EventLog')。不,上面的代碼不會生成事件日誌條目。如果您啓用審覈(您需要執行以獲取修改文件的信息),該審覈已經創建了事件日誌記錄。 –