无法将Powershell输出写入CSV文件

时间:2019-05-08 21:19:37

标签: powershell

我是PowerShell的新手,试图编写一个简单的脚本来查找特定用户登录工作站的次数,并将该信息以一种有用的方式导出到CSV文件中,以便可以轻松地对其进行操作。 CSV文件仅真正需要包含登录时间和安全日志条目的“消息”部分中提到的用户名。

我的问题是,似乎可以获取带有包含用户名的截短“消息”的CSV文件,或者可以获取要打印托管的所有信息,而不是导出为CSV。我确定该解决方案可能非常基础,但是就像我说的我是新手。

在这里发布的代码中,我可以打印所有需要托管的内容,但是我似乎无法将其保存到CSV文件中。任何帮助将不胜感激。

New-Item -Name "UserLoginHistory" -Path C:\ -ItemType Directory -Force | Out-Null
$UserName = Read-Host -Prompt 'Which user are you searching for?'
$a =Get-EventLog -LogName Security -Message "*$UserName*" | Where-Object {$_.EventID -eq 4624} 
foreach($item in $a)
{
        $timeLog = $item.TimeGenerated
     $item = $item.Message.Split(":")

     $subject = $item[3].split()
     #$subject[2]
     $NewLogin = $item[14].split()
     #$NewLogin[2]
     $WorkstationName = $item[26].split()
     #$WorkstationName[1]
     $SourceNetworkAddress = $item[27].split()
     #$SourceNetworkAddress[1]


    "Time: $timeLog Subject: $($subject[2]) NewLogin: $($NewLogin[2]) WorkstationName $($WorkstationName[1]) SourceNetworkAddress $($SourceNetworkAddress[1])"

}
 Export-Csv -Path C:\UserLoginHistory\LoginHistory.csv

2 个答案:

答案 0 :(得分:1)

  • 请勿在{scrript块}中重用foreach的$ item变量用于其他目的。
  • 创建一个[PSCustomObject]并将其发送给整个foreach的收集变量

未经测试的模板:

New-Item -Name "UserLoginHistory" -Path C:\ -ItemType Directory -Force | Out-Null
$UserName = Read-Host -Prompt 'Which user are you searching for?'
$Events = Get-EventLog -LogName Security -Message "*$UserName*" | Where-Object {$_.EventID -eq 4624} 

$Data = foreach($Event in $Events){
    $item = $Event.Message.Split(":")
    [PSCustomObject]@{
        Time                 = $Event.TimeGenerated
        Subject              = $item[3].split()[2]
        NewLogin             = $item[14].split()[2]
        WorkstationName      = $item[26].split()[1] 
        SourceNetworkAddress = $item[27].split()[1]
    }
}
$Data | Format-Table -Autosize *
$Data | Out-Gridview
$Data | Export-Csv -Path C:\UserLoginHistory\LoginHistory.csv -NoTypeInformation

答案 1 :(得分:0)

尝试将结果填充到未经测试的代码中。

    New-Item -Name "UserLoginHistory" -Path C:\ -ItemType Directory -Force | Out-Null
$UserName = Read-Host -Prompt 'Which user are you searching for?'
$a =Get-EventLog -LogName Security -Message "*$UserName*" | Where-Object {$_.EventID -eq 4624} 
$ReportOutPut = @() # An array to hold your output.
foreach($item in $a)
{
        $timeLog = $item.TimeGenerated
     $item = $item.Message.Split(":")

     $subject = $item[3].split()
     #$subject[2]
     $NewLogin = $item[14].split()
     #$NewLogin[2]
     $WorkstationName = $item[26].split()
     #$WorkstationName[1]
     $SourceNetworkAddress = $item[27].split()
     #$SourceNetworkAddress[1]


   "Time: $timeLog Subject: $($subject[2]) NewLogin: $($NewLogin[2]) WorkstationName $($WorkstationName[1]) SourceNetworkAddress $($SourceNetworkAddress[1])"

    $ReportOutput += [pscustomobject] @{
        Time = $timeLog;
        Subject = $subject[2];
        NewLogin = $NewLogin[2];
        WorkstationName =  $WorkstationName[1];
        SourceNetworkAddress = $SourceNetworkAddress[1]
        } # Custom objec to be exported via csv

    }

Export-Csv -InputObject $ReportOutPut -NoTypeInformation -Path C:\UserLoginHistory\LoginHistory.csv