繁体   English   中英

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

[英]Powershell Get all groups of ADusers into unique columns in a CSV

下午好,

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

脚本的最终目标:有一个excel文件,其中包含AD属性名称,Office,组织的列,最重要的是,用户所属的每个组的单独列。 我遇到的问题是为用户拥有的每个/每个组创建一个新列。 并非所有用户都有相同数量的组。 有的有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

}

我想出了这个解决方案,只需循环$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

希望这可以帮助。

暂无
暂无

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

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