简体   繁体   English

从多个组中导出用户

[英]Export user from multiple groups

Good Morning Guys,早上好家伙,

I write this script to export multiple users in multiple groups in a CSV file.我编写此脚本以在 CSV 文件中导出多个组中的多个用户。

In addition to not exporting anything, I cannot insert a column in the CSV where the user's appatenency group is specified.除了不导出任何内容之外,我无法在 CSV 中插入一列,其中指定了用户的 appatenency 组。 Just the one with the name "vpn-*", like vpn-users只是名称为“vpn-*”的那个,例如 vpn-users

Powershell Version 4.0 Powershell 版本 4.0

$Data = $UserData = @()
$GroupName = Get-ADGroup -Filter {name -like "VPN-*"} -Properties Name | Select-Object Name
Foreach ($group in $GroupName)
{
$UserData = Get-ADGroupMember -Identity {$group}
Where objectClass -eq 'user' | 
Get-ADUser -Properties Name, UserPrincipalName, description, Enabled |
Select-Object Name, UserPrincipalName, description, Enabled 
}
$UserData  | export-csv "c:\members2.csv"

I see a number of issues with the posted code.我看到发布的代码存在许多问题。 Some are mere preferences, but others look like they would affect the outcome.有些只是偏好,但有些看起来会影响结果。

-Properties Name isn't needed in the initial Get-ADGroup command. - 初始Get-ADGroup命令中不需要属性-Properties Name The Name property is returned by default.默认情况下返回 Name 属性。

Respectful to @Theo's comment re: -ExpandProperty , however, I prefer not to use that unnecessarily.尊重@Theo 的评论: -ExpandProperty但是,我不想不必要地使用它。 You don't even need the first Select-Object command, just reference the property and it will unroll and return a string array.您甚至不需要第一个Select-Object命令,只需引用该属性,它就会展开并返回一个字符串数组。 Property unrolling is supported as of PowerShell Version 3.从 PowerShell 版本 3 开始支持属性展开。

Your argument to the -Filter parameter should be a string. -Filter参数的参数应该是一个字符串。 I myself have a hard time breaking the habit of wrapping those queries in script blocks {...} , however, there are some specifics of the AD cmdlets, and using a string is a best practice.我自己很难打破将这些查询包装在脚本块{...}中的习惯,但是,AD cmdlet 有一些细节,使用字符串是最佳实践。 Basically, the argument takes a string type, if you give it a script block it will convert it to a string and that process may not always work as desired.基本上,该参数采用字符串类型,如果您给它一个脚本块,它会将其转换为字符串,并且该过程可能并不总是按预期工作。

It's better to store your AD properties in an array you can cite multiple times.最好将您的 AD 属性存储在可以多次引用的数组中。 Normally you would've only needed to add 'Description' but considering you were also using it in a later Select-Object command it makes more sense to add all the desired properties to an array.通常您只需要添加“描述”,但考虑到您也在稍后的Select-Object命令中使用它,将所有所需的属性添加到数组中更有意义。

Personally, I prefer not to use simplified syntax.就个人而言,我不喜欢使用简化的语法。 I don't know what the community's opinion is on that but I rewrote using my preference.我不知道社区对此有何看法,但我根据自己的喜好重写了。

You were also missing |你也失踪了| ' before the Where-Object command. ' 在Where-Object命令之前。

And, yes you would have to move the Export-Csv inside the loop to output data per loop iteration.而且,是的,您必须在每次循环迭代时将循环内的Export-Csv移动到 output 数据。 Using the -Append parameter to avoid overwriting it on every iteration.使用-Append参数避免在每次迭代时覆盖它。

Untested Example:未经测试的例子:

$ADProps = 'Name', 'UserPrincipalName', 'description', 'Enabled'

$GroupName = (Get-ADGroup -Filter "name -like 'VPN-*'" ).Name

Foreach ( $Group in $GroupName )
{
    $UserData = 
    Get-ADGroupMember -Identity $Group |
    Where-Object{ $_.objectclass -eq 'user' } | 
    Get-ADUser -Properties $ADProps |
    Select-Object ($ADProps += @{Name = 'Group'; Expression = { $Group }})

    $UserData | export-csv "c:\members2.csv" -NoTypeInformation -Append
}

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

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