简体   繁体   English

Powershell脚本提取AD用户组成员资格但CSV格式化乱七八糟

[英]Powershell script pulling AD Users Group Memberships but CSV formatting a mess

I am trying to create a csv file which contains the username and the group memberships from a list of usernames to a CSV file. 我正在尝试创建一个csv文件,其中包含用户名和组成员资格,从用户名列表到CSV文件。

But the CSV file is formatted as below 但CSV文件的格式如下

******;"CN=Cal_ACABLHolidayCalendar_Editor

Username and group in 1 cell 1个单元格中的用户名和组

$list = Get-Content c:\Tools\Powershell\ACA\userlist.txt
$list | `
    %{  
        $user = $_; 
        get-aduser $user -Properties memberof | `
        select -expand memberof | `
        %{new-object PSObject -property @{User=$user;Group=$_;}} `
    } |
export-csv –Path "C:\Tools\Powershell\ADUsers.csv" -Delimiter ‘;’ –NoTypeInformation -Encoding UTF8

How can I have the formatting as the first cell with the username and the below cells with the groups 如何将格式设置为具有用户名的第一个单元格以及具有组的以下单元格

Export-CSV is the wrong way to achieve your desired solution. Export-CSV是实现所需解决方案的错误方法。
You should be better with something like this: 你应该做得更好:

$excelfile = "C:\Daten\test.xlsx"

###Excel Parameter###
$excel = New-Object -COM "Excel.Application"    
$excel.displayalerts = $false
$excel.visible = $false
$excel.usercontrol = $false         
$Workbook=$excel.Workbooks.open($excelfile)     
$Worksheet=$Workbook.Worksheets.Item(1)
$Worksheet.Activate() | Out-null


$x = 1
$y = 1

$list = get-content c:\daten\list.txt
foreach ($user in $list){
 $groups = (get-aduser $user -Properties memberof | select -expand memberof | %{new-object PSObject -property @{User=$user;Group=$_;}} | select -ExpandProperty Group)
 $z = $groups.count
 $excel.cells.item($y,$x) = $user
 for ($i = 2; $i -le $groups.count+1; $i++)
 {
 $excel.cells.item($i,$x) = $groups[$i]
 }
 $x=$x+1
}
$Workbook.Save()
$Workbook.Close()
get-process *excel* | kill -Force

Its not 100% correct. 它不是100%正确。 Something is wrong with the counter of the username. 用户名的计数器有问题。 First user works good though. 第一个用户工作得很好。

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

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