2014-05-22 2 views
2

Windows 이벤트 로그에서 로그온 및 로그 오프 이벤트를 읽고 Powershell을 사용하여 Active Directory에서 각 사용자에 대한 해당 정보를 검색하는 방법은 무엇입니까?Powershell을 사용하여 로그온 이벤트를 읽고 사용자 정보를 찾는 방법은 무엇입니까?

+0

왜 질문을 게시 ??? – lucianomarisi

+0

누군가 다른 사람이 물어보고 싶다면 대답은 이미 있습니다. –

+0

@AutumnBaril 귀하의 질문과 답변이 유익하고 +1 질문을 찾았습니다. – Colyn1337

답변

3

다음 스크립트는 시스템 로그에서 Winlogon 이벤트를 읽고 각 사용자의 SID를 기반으로 AD에서 정보를 검색하며 생성 된 HTML 페이지에 결과를 표시합니다. 각 AD 조회의 결과는 AD 서버로의 불필요한 왕복을 방지하기 위해 캐시됩니다. 당신이 대답을 찾고하지 않는 경우

# event id 7001 is Logon, event id 7002 is Logoff 
function WinlogonEventIdToString($EventID) {switch($EventID){7001{"Logon";break}7002{"Logoff";break}}} 

# look up SID in Active Directory and cache the results in a hashtable 
$AdUsers = @{} 
function SidToAdUser($sid) { 
    $AdUser = $AdUsers[$sid] 
    if ($AdUser -eq $null) { 
    $AdUser = $AdUsers[$sid] = [adsi]("LDAP://<SID=" + $sid + ">") 
    } 
    return $AdUser 
} 

$outputFilename = [System.IO.Path]::GetTempPath() + "DisplayLatestLogonEvents.html" 

# the first Select extracts the SID from the event log entry and converts the event id to a descriptive string 
# the second Select is responsible for looking up the User object in Active Directory, using the SID 
# the final Select picks the various attribute data from the User object, ready for display in the table 
# to retrieve only recent log entries, one can use something like this in Get-EventLog: -After (Get-Date).AddDays(-14) 
Get-Eventlog -Logname "System" -Source "Microsoft-Windows-Winlogon" -InstanceId 7001,7002 ` 
    | Select TimeGenerated, @{n='Operation';e={WinlogonEventIdToString $_.EventID}}, @{n='SID';e={$_.ReplacementStrings[1]}} ` 
    | Select TimeGenerated, Operation, @{n='AdUser';e={(SidToAdUser $_.SID)}} ` 
    | Select TimeGenerated, Operation, ` 
      @{n='Username';e={$_.AdUser.sAMAccountName}}, ` 
      @{n='Full name';e={$_.AdUser.firstname + " " + $_.AdUser.lastname}}, ` 
      @{n='Title';e={$_.AdUser.title}}, ` 
      @{n='Department';e={$_.AdUser.department}}, ` 
      @{n='Company';e={$_.AdUser.company}} ` 
    | ConvertTo-HTML -Head "<style>td, th { border:1px solid grey }</style>" | Out-File $outputFilename 

# this will open the default web browser 
Invoke-Expression $outputFilename 
관련 문제