[Powershell] Foreach pipline [英] [Powershell] Foreach pipline

查看:93
本文介绍了[Powershell] Foreach pipline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


我做了以下功能

 function LastLogon($ computername){

$ results = @()
foreach($ c in $ computername)
{
$ results + = write-output'计算机:'$ c
$结果+ = Get-EventLog -LogName安全性-InstanceId 528 -ComputerName $ c |
其中{$ _.UserName -ne" NT AUTHORITY \ NETWORK SERVICE"} |
选择TimeGenerated,用户名-First 1
}
$ results | Export-Csv -NoTypeInformation -Path C:\scripts\lastlogon.csv
}

我按照lastlogon -computername运行它(get-content c: \ computers.txt)


我得到以下结果


" Length"

" 9"

" 12"


需要更改以获得具有机器名,登录时间和用户登录信息的正确.csv文件?


解决方案


不要使用


结果变量,创建自定义对象或通过计算属性添加计算机名称。


您还应该考虑使用Get-WinEvent或至少使用Get-EventLog以更有效的方式。


我还要重写它以使用ForEach-Object循环而不是foreach,因为我发现那些更容易输出。


hi

i made the following function

 function LastLogon ($computername) {

    $results = @()
    foreach ($c in $computername)
    { 
      $results += write-output 'Computer:' $c
      $results += Get-EventLog -LogName Security -InstanceId 528 -ComputerName $c | 
       where {$_.UserName -ne "NT AUTHORITY\NETWORK SERVICE"} | 
       select TimeGenerated, username -First 1
    }  
    $results | Export-Csv -NoTypeInformation -Path C:\scripts\lastlogon.csv
}

i run it as follow lastlogon -computername (get-content c:\computers.txt)

i got the following result

"Length"
"9"
"12"

what need to change to got the correct .csv file with machine name, logon time and user login?

解决方案

Hi,

Don't use your


results variable, create a custom object or add the computer name via a calculated property instead.

You should also look at using Get-WinEvent instead or at least using Get-EventLog in a more efficient manner.

I'd also rewrite this to use a ForEach-Object loop instead of a foreach, as I find those much easier to output from.


这篇关于[Powershell] Foreach pipline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆