Powershell记录过程

Powershell记录过程

问题描述:

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

我需要脚本从主机名或名称列表中拉我在这里尝试这个。

$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 

现在我有,我找这个保存在目录中我告诉它与名称的文件被真正地奋斗部分:安全-LOG-DD-MM-YY。这是我所拥有的

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

当我这样运行脚本时,它要求我输入对象。

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

脚本的问题是,它不是将数据写入文件,而是提示输入对象?

+0

我们尽力帮你(这里)(http://*.com/questions/37188496/file-location-for-csv-file)了。请记住,您可以编辑和/或添加到您的原始问题,而不是以多个线索和方式询问同一问题。 ;) – gravity

+0

当你问如何“做得更好”时,这被认为是模糊的和偏离主题的。你的问题应该有针对性地询问如何获取写入文件的数据,因为它不起作用,并提示你输入InputObject。 –

+0

谢谢我在这里继续学习发帖,谢谢 – Syseng

您不会将任何内容传递给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"))