2

下午好,

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

脚本的最终目标:拥有一个 Excel 文件,其中包含 AD 属性 Name、Office、Org 的列,最重要的是,用户所属的每个组都有一个单独的列。我遇到的问题是为用户拥有的每个/每个组创建一个新列。并非所有用户都有相同数量的组。有些有 10 个,有些有 30 个(是的 30,我们的 AD 是一团糟)。这是我到目前为止所做的,而我遇到困难的地方是接近尾声:

$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

}
4

1 回答 1

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

希望这可以帮助。

于 2018-08-07T21:11:51.913 回答