3

リモートコンピューターのユーザーログオン/ログオフセッションと時間を確認できるようにしたいのですが、stackoverflowから取得した次のコードがありますが、リモートコンピューターを確認するようにスクリプトに指示する方法がわかりません。

$UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier  
$_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
$TimeProeprty = @{n="Time";e={$_.TimeGenerated}}

Get-EventLog System -Source Microsoft-Windows-Winlogon | select $UserProperty,$TypeProperty,$TimeProeprty

次のように$Computername変数とForeachループステートメントをスローして、リモートコンピューターで実行しようとしましたが、リモートシステムではなく、使用しているローカルシステムをチェックし続けます。

$Computername = Read-Host "Enter Computername Here"

Foreach $Computer in $Computername

    {
        $UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
        $TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
        $TimeProeprty = @{n="Time";e={$_.TimeGenerated}}

        Get-EventLog System -Source Microsoft-Windows-Winlogon | select $UserProperty,$TypeProperty,$TimeProeprty
    }
4

6 に答える 6

2

You need to use the Get-EventLog cmdlet's ComputerName parameter:

Get-EventLog -ComputerName $Computer System -Source Microsoft-Windows-Winlogon `
    | select $UserProperty,$TypeProperty,$TimeProeprty

Also, it looks like you have a typo in your $TimeProeprty variable.

于 2012-08-01T16:30:59.660 に答える
2

少し変更され、その動作

# Specify the location you want the report to be saved
$filelocation = "C:\report.csv"

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') 
[string]$Computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter     ComputerName", "Computer Name", "Computer Name")
[int]$DayPrompt = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Number of Days to     check", "Days to Check", "15")
$Days = $DayPrompt
cls

$Result = @()
Write-Host "Gathering Event Logs, this can take awhile..."
$ELogs = Get-EventLog System -Source Microsoft-Windows-WinLogon -After (Get-Date).AddDays(-    $Days) -ComputerName $Computer
If ($ELogs)
{ Write-Host "Processing..."
ForEach ($Log in $ELogs)
{ If ($Log.InstanceId -eq 7001)
{ $ET = "Logon"
}
ElseIf ($Log.InstanceId -eq 7002)
{ $ET = "Logoff"
}
Else
{ Continue
}
$Result += New-Object PSObject -Property @{
Time = $Log.TimeWritten
'Event Type' = $ET
User = (New-Object System.Security.Principal.SecurityIdentifier $Log.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])
}
}
$Result | Select Time,"Event Type",User | Sort Time -Descending | Export-CSV $filelocation
Write-Host "Done look at $filelocation"
}
Else
{ Write-Host "Problem with $Computer."
Write-Host "If you see a 'Network Path not found' error, try starting the Remote Registry service on that computer."
Write-Host "Or there are no logon/logoff events (XP requires auditing be turned on)"
}
于 2016-05-01T09:12:44.467 に答える
1

ループ内のコマンドにコンピューター名を渡していません。したがって、$computerName にあるオブジェクトの数だけ、同じコマンドをループするだけです。最後の行を次のように変更してみてください。

 Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer | select $UserProperty,$TypeProperty,$TimeProperty

それでもうまくいかない場合は、foreach ループが正しいデータを渡していることを確認してください。

$computerName | Foreach-Object{Write-Host $_}

これを実行しようとしている各マシンのコンピューター名が表示されます。

ただし、1 台のコンピューターに対して実行しようとしているように見えるので、Foreach ループを削除し、-ComputerName $computernameselect ステートメントの前に Get-Eventlog コマンドの末尾に追加するだけです

于 2012-08-01T16:30:36.007 に答える
1

https://gallery.technet.microsoft.com/Log-Parser-to-Identify-8aac36bdに基づく

Get-Eventlog -LogName Security | where {$_.EventId -eq "4624"} | select-object @{Name="User"
;Expression={$_.ReplacementStrings[5]}} | sort-object User -unique

ReplacementStrings から他の情報を取得できます。Get-Eventlog コマンドでリモート コンピューターを指定することもできます。

于 2016-07-27T20:29:10.213 に答える