2016-10-05 4 views
1

W2012を実行しているQAマシンでIO.FileSystemWatcherウォッチャをテスト用に使用しています。Powershell-ファイル監査人がWindowsユーザ名を取得していない場合ログインした人

- PSスクリプトは、新しいユーザーがマシンでログインするたびにスケジュールタスクとして実行されます。 - PSスクリプトはログファイルをキャプチャし、すべての変更(作成、名前変更、削除、変更)時に電子メールを送信します。

PowerShellのスクリプトSystemFileMonitorではなく、変更を行ったユーザー名をキャプチャされていない、常にスケジュールタスクを実行しているユーザー名を取得している例外を除いて、うまく動作します。

アイデア?私もサービスの所有者をキャプチャしようとしましたが、それでも同じ問題です。ここで

は、コードは次のとおりです。

*$location = Get-location 
$machine = [Environment]::MachineName 
$userLogged = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name 
$userLogged1 = (Get-WmiObject Win32_Process -Filter  "Name='explorer.exe'").getOwner() | Select User 
$folder = "C:\apache-tomcat-8.0.33\webapps" 
$filter = "*.ini" 

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
IncludeSubdirectories = $false NotifyFilter = [IO.NotifyFilters]'FileName,  LastWrite' 
} 

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action{ 
$path = $Event.SourceEventArgs.FullPath 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 

Write-Host "The file '$name' was $changeType at $timeStamp" and $userLogged and Process Owner '$userLogged1' -fore green 

Out-File -FilePath $location\logs\INI-outlog-Created.txt -Append -InputObject "The file '$name' was '$changeType' at '$timeStamp' on machine '$machine' on Path:'$path' , by user '$userLogged' Process Owner '$userLogged1'" 

#SEND EMAIL# 
$From = "[email protected]" 
$To = "[email protected]" 
$Cc = "[email protected]" 
$Attachment = "$location\logs\INI-outlog-Created.txt" 
$Subject = "File Created – CHANGE ALERT SERVERTRUNK" 
$Body = "File Created @ SERVERTRUNK – The file '$name' was '$changeType' at '$timeStamp' on machine '$machine' on Path:'$path' , by user '$userLogged' Process Owner '$userLogged1'" 
$SMTPServer = "MailServer" 

Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment -Priority High -dno onSuccess, onFailure}* 

は常に我々はログインが以下のように(サーバ上でスケジュールタスクを実行しているユーザー名を)DUMP取得:

ファイル「と改名 - Copy.ini」 「@ C:\ Monitor1 \ Source1 \ rennamed-Copy.ini」、ユーザ別:'@ {User = SVC_TEST}'のパス上のマシン 'TESTQA'の'10/03/2016 10:11:36 ' 「

はいただきありがとうございますyアイデア。 Paulo。

+1

'$のuserlogged'は、現在のユーザーに設定されています。スクリプトを実行したときにユーザーがログインしていない場合は、代わりにどのユーザーに期待していますか? 「*変更を行ったユーザー名*」というフレーズはあいまいです。スケジュールされたタスクがどのようにトリガされているかはわかりませんが、そのときにユーザー名を保存する必要があります。 –

+0

'$ userlogged1'の代わりに何を言うのですか? –

+0

@RyanBemrose:あなたの答えをありがとう。はい、私はログインしているユーザーを期待していますが、他のユーザーがそのマシンにログインして変更を行った場合、キャプチャされたユーザーはログインしていないSCタスクを実行しているユーザーです。SCタスクは、 C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe -noexit -noprofile -executionpolicy bypass -file "C:\ Monitor1 \ Full-3folders.ps1"というバッチファイルを呼び出します。 – Paulo

答えて

関連する問題