Powershell将所有ADuser组放入CSV的唯一列中

时间:2018-08-07 20:49:08

标签: powershell active-directory export-csv

下午好,

我搜索了这个论坛,并搜索了其他几个结合了想法并尝试不同角度的东西,但是还没有弄清楚。如果已经回答了这个问题,并且我很想搜索,很抱歉,请告诉我。

脚本的最终目标:有一个excel文件,其中包含AD属性名称,Office,Org的列,最重要的是,用户所属的每个组的单独列。 我遇到的问题是为用户拥有的每个/每个组创建一个新列。并非所有用户都有相同数量的组。有的有10个,有的有30个(是的30,我们的广告很混乱)。 这是我到目前为止所做的,而我遇到的困难即将结束:

$scoop = get-content C:\temp\SCOOP.txt ##This is a text file with a list of user id's, to search AD with
$outfile = 'C:\temp\SCOOP_ID.csv'
$ou = "OU=Humans,OU=Coupeville,DC=ISLANDS"  #This is the searchbase, helps AD isolate the objects
Clear-Content $outfile #I clear content each time when I am testing 

Foreach($ID in $scoop){
##AD Search filters##
$filtertype = 'SamAccountName'
$filter1 = $ID
##End AD Search filters##

##AD Search --MY MAIN ISSUE is getting the MemberOF property properly
$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization,MemberOf    

##AD Search ## Turns the MemberOf property to a string, I tried this during my testing not sure if objects or strings are easier to work with
#$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization, @{n='MemberOf'; e= { $_.memberof | Out-String}}  


#Attempt to specify each property for the output to csv
$name = $properties.Name
$office = $properties.Office
$org = $properties.Organization
$MemberOf = $properties.MemberOf 


$membersArray = @()
foreach($mem in $MemberOf){ $membersArray += $mem }


###This is what I typically use to export to a CSV - I am sure there are other maybe better ways but this one I know. 
$Focus = [PSCustomObject]@{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value $name -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Office' -Value $office -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Org' -Value $org -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Groups' -Value $MemberOf -Force 

<########################
#
#     Main ISSUE is getting the $memberOf variable, which contains all of the users groups, into seperate columns in the csv. To make it more plain, each column would be labeled 'Groups', then 'Groups1', and so on.

I have tried a few things but not sure if any were done properly or what I am messing up. I tried using $memberof.item($i) with a FOR loop but couldnt figure out how to send each
item out into its own Add-Member property. 

#
#######################>

##Final Output of the $Focus Object
$Focus | Export-Csv $outfile -Append -NoTypeInformation

}

1 个答案:

答案 0 :(得分:0)

我想出了这个解决方案,只需循环$MemberOf并添加一个唯一的名称即可。

$MemberOf = @('a','b','c','d','e','f') #example

$Focus = [PSCustomObject]@{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force

$i=0; $MemberOf | % {
    $i++
    $Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force 
}

$Focus | Export-Csv xmgtest1.csv -Append -Force -NoTypeInformation

但是,这不适用于您的情况,因为您为每个用户运行一次此代码,并且没有整个集合的“意识”。因此(在您现有的体系结构中)您需要确保首先添加最大数量的组。

您可以通过一次全部创建CSV来解决此问题(唯一的选择是不断加载/卸载csv文件)。

首先,将$csv = @()添加到文件顶部。

然后,在完成创建$Focus之后,将其添加到$csv

最后,您可以删除-Append

缩小版看起来像这样:

$csv = @()

#Foreach($ID in $scoop){ etc..

$MemberOf = @('a','b','c','d','e','f') #example

$Focus = [PSCustomObject]@{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force
#$Focus | Add-Member etc ...
$i=0; $MemberOf | % {
    $i++
    $Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force 
}

$csv += $Focus

# } end of Foreach($ID in $scoop)

$csv | Export-Csv xmgtest1.csv -NoTypeInformation

希望这会有所帮助。