Powershell日志记录过程

时间:2016-05-12 19:47:13

标签: powershell

我要求审核每周收集所有安全日志,当我到达我的新工作时,当前系统管理员会手动登录每台机器并抓取日志保存并存储它们,我知道有更好的方式(我们有50多台机器)我和它一起使用了一段时间,因为我是新人,但我和我的经理谈到自动化它,他认为这是一个好主意。所以我一直在研究Power shell脚本来做到这一点。这是我到目前为止所拥有的。

我需要脚本从我在这里尝试的主机名或名单中提取。

$computers = get-content C:\MyScripts\MachineNames.txt
foreach-object ($computer in $computers)
{

这里我提到最近7天的时间,只记录失败的登录尝试的安全日志。

$Date = [DateTime]::Now.AddDays(-7)
$Date.tostring("MM-dd-yyyy"), $env:Computername
Get-EventLog "Security" -After $Date 
| Where -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -notlike "*$"} `
| foreach-Object 
$row = "" | Select UserName, LoginTime
$row.UserName = $_.ReplacementStrings[5]
$row.LoginTime = $_.TimeGenerated
$row # this will put the current row to the pipeline

现在,我一直在努力的部分,我正在寻找这个文件保存在我告诉它的目录中的名称:Security-Log-dd-mm-yy。这就是我所拥有的

Export-Csv ('C:\logs\security-log-{0}.csv' -f ([DateTime]::Now).ToString("MM-dd-yyyy"))

此时我运行这样的脚本,它要求我输入Object。

cmdlet Export-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject: 

脚本有什么问题,它没有将数据写入文件,而是提示输入对象?

1 个答案:

答案 0 :(得分:0)

您不会将任何内容传递给Export-Csv cmdlet。你可能想做这样的事情:

Get-EventLog "Security" -After $Date `
| Where -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -notlike "*$"} `
| foreach-Object {
    $row = "" | Select UserName, LoginTime
    $row.UserName = $_.ReplacementStrings[5]
    $row.LoginTime = $_.TimeGenerated
    $row # this will put the current row to the pipeline 
} | Export-Csv ('C:\logs\security-log-{0}.csv' -f ([DateTime]::Now).ToString("MM-dd-yyyy"))