Powershell 将所有 ADuser 组放入 CSV 中的唯一列中
Powershell Get all groups of ADusers into unique columns in a CSV
下午好,
我搜索了这个论坛和其他几个结合想法并尝试不同角度的论坛,但还没有弄清楚。如果已经回答了,而我搜索很烂,很抱歉,请告诉我。
脚本的最终目标:有一个 excel 文件,其中包含 AD 属性名称、办公室、组织的列,最重要的是,用户所属的每个组都有一个单独的列。
我 运行 遇到的问题是为用户拥有的 each/every 组创建一个新列。并非所有用户都拥有相同数量的组。有些有 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
然而,这在您的情况下不起作用,因为您 运行 每个用户一次此代码,并且整个集合中没有 "awareness"。因此,您(在现有架构中)需要确保首先添加最大数量的组。
您可以通过一次创建所有 CSV 来解决此问题(唯一的选择是不断 load/unload 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
希望对您有所帮助。
下午好,
我搜索了这个论坛和其他几个结合想法并尝试不同角度的论坛,但还没有弄清楚。如果已经回答了,而我搜索很烂,很抱歉,请告诉我。
脚本的最终目标:有一个 excel 文件,其中包含 AD 属性名称、办公室、组织的列,最重要的是,用户所属的每个组都有一个单独的列。 我 运行 遇到的问题是为用户拥有的 each/every 组创建一个新列。并非所有用户都拥有相同数量的组。有些有 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
然而,这在您的情况下不起作用,因为您 运行 每个用户一次此代码,并且整个集合中没有 "awareness"。因此,您(在现有架构中)需要确保首先添加最大数量的组。
您可以通过一次创建所有 CSV 来解决此问题(唯一的选择是不断 load/unload 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
希望对您有所帮助。