简体   繁体   English

Get-ADuser并导出到多个CSV文件

[英]Get-ADuser and export to multiple CSV files

I'm using the following command to grab users from an OU and export to csv file: 我正在使用以下命令从OU抓取用户并将其导出到csv文件:

Get-ADUser -Filter * -SearchBase 'OU=Contoso Users,OU=Contoso,DC=domain,DC=local' -Properties * | Select UserPrincipalName, EmailAddress | Sort UserPrincipalName | Export-CSV $UsersToMigrate -NoTypeInformation -Force

Is there anyway to export to multiple csv files of 10 users per file? 反正有导出到每个文件10个用户的多个csv文件吗?

Append to the respective output file in a loop and use a counter and integer division to determine the actual filename. 循环附加到相应的输出文件,并使用计数器和整数除法确定实际的文件名。

$i = 0
... | Sort UserPrincipalName | ForEach-Object {
    $csv = "C:\path\to\output_$([Math]::Floor([int]$i/[int]10)).csv"
    $_ | Export-Csv $csv -NoType -Append
    $i++
}
$Users = Get-ADUser -Filter * -SearchBase 'OU=Contoso Users,OU=Contoso,DC=domain,DC=local' -Properties * | Select UserPrincipalName, EmailAddress | Sort UserPrincipalName
$Users | ForEach-Object -Begin {$i = 1} {
     $_ | Export-CSV "$UsersToMigrate-$([Math]::Ceiling($i++ / 10)).csv" -NoTypeInformation -Append
}

Explanation 说明

  • Iterates through the collection of users with ForEach-Object , initialising a counter variable $i as 1 in a Begin block first. 使用ForEach-Object遍历用户集合, BeginBegin块中将计数器变量$i初始化为1。
  • Divides the counter by 10 and rounds up to the nearest integer. 将计数器除以10,然后四舍五入到最接近的整数。 Uses this as part of the CSV name and exports to the CSV with the -Append switch (requires PSv3+ I believe). 使用它作为CSV名称的一部分,并通过-Append开关导出到CSV(我相信需要PSv3 +)。

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

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