简体   繁体   English

如何使用Powershell将foreach输出到CSV?

[英]How to output foreach into CSV using Powershell?

I am displaying all users in a group with this: 我正在显示组中的所有用户:

Import-Module ActiveDirectory

$Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name   # UC_* is my group

foreach ($Group in $Groups){
Write-Output "Group" 
Write-Output "-----"
$Group
Write-Output ""
Get-ADGroupMember -Identity $Group | select -Property Name, samaccountname 
Write-Output ""
}

I want to output this to a CSV but when I do, it looks like each line is getting overwritten by the next line in the loop so there is no data in the CSV. 我想将其输出到CSV但是当我这样做时,看起来每一行都被循环中的下一行覆盖,因此CSV中没有数据。 This is not working properly: 这不能正常工作:

# Get users in a group or groups

Import-Module ActiveDirectory


Function Get-Users {
    $Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name   # UC_* is my group

    foreach ($Group in $Groups){
    Write-Output "Group" 
    Write-Output "-----"
    $Group
    Write-Output ""
    Get-ADGroupMember -Identity $Group | select -Property Name, samaccountname 
    Write-Output ""
    }
}

Get-Users | export-csv "c:\temp\myfile.csv"

How can I output all content to a CSV properly? 如何正确地将所有内容输出到CSV?

The output of your function is Write-output 's, $Group , and the objects returned from Get-ADGroupMember . 函数的输出是Write-output$GroupGet-ADGroupMember返回的对象。 This collection of different objects makes it not able to be exported by the Export-CSV , but would be something for a text document via Out-File . 这个不同对象的集合使它无法通过Export-CSV ,但可以通过Out-File Export-CSV文本文档。

If you want to export to a csv, you need to create a collection of consistent objects with the properties you want to export: 如果要导出到csv,则需要使用要导出的属性创建一致对象的集合:

So we'll loop over each group with ForEach-Object , and store the group membership in $members . 因此,我们将使用ForEach-Object循环遍历每个组,并将组成员身份存储在$members Then can loop over that with foreach , this means that we can still use the information from the ForEach-Object loop in the $_ to get the name of the group, and the user information in $member and create an object for every user with just the information that's needed by making a [pscustomobject] 然后可以使用foreach循环foreach ,这意味着我们仍然可以使用来自$_ForEach-Object循环的信息来获取组的名称,以及$member的用户信息并为每个用户创建一个对象只需要制作[pscustomobject]所需的信息

Get-ADGroup -Filter "name -like 'UC_*'" |
    ForEach-Object {
        $members = Get-ADGroupMember $_
        foreach ($member in $members) {
            [pscustomobject]@{
                "Group Name"     = $_.Name
                "SamAccountName" = $member.SamAccountName
                "User Name"      = $member.name
            }
        }
    } |
    Export-Csv "c:\temp\myfile.csv"

Also as @mklement0 mentioned, it's best practice to not use scriptblock's with the filters on the AD cmdlets. 同样如@ mklement0所述,最佳做法是不要将scriptblock与AD cmdlet上的过滤器一起使用。 His excellent answer here gives details why. 他出色的答案给出了详细解释。

How about this: 这个怎么样:

$Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name   # UC_* is my group

$Groups | Export-Csv  "c:\temp\myfile.csv" -NoTypeInformation

There are two options for outputting to a CSV. 输出到CSV有两种选择。 Using Export-CSV will overwrite a file that is already there. 使用Export-CSV将覆盖已存在的文件。 So you can put everything into a variable and output that to a CSV or you can add -Append to the Export-CSV cmdlet call. 因此,您可以将所有内容放入变量并将其输出到CSV,也可以将-Append添加到Export-CSV cmdlet调用。 This will append data rather than overwrite it. 这将附加数据而不是覆盖它。

Store the data you're parsing in your function in an object. 将您正在解析的数据存储在对象中。

For example, change this... 例如,改变这个......

$Groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name   # UC_* is my group

foreach ($Group in $Groups){
Write-Output "Group" 
Write-Output "-----"
$Group
Write-Output ""
Get-ADGroupMember -Identity $Group | select -Property Name, samaccountname 
Write-Output ""

... to something like this ... ......这样的事......

$users = @()

$groups = Get-ADGroup -Filter {name -like "UC_*"} | Select -ExpandProperty Name

$groups | % {

    $group | New-Object System.Object
    $group | Add-Member -MemberType NoteProperty -Name GroupName -Value $_

        Get-ADGroupMember -Identity $_ | Select -Property Name, samaccountname | % {

$group | Add-Member -MemberType NoteProperty -Name MemberName -Value $_.Name
$group | Add-Member -MemberType NoteProperty -Name samaccountname -Value $_.samaccountname


}


    $users += $group

}

I haven't tested the code above, but hopefully it helps. 我没有测试过上面的代码,但希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM