gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:09:49 24 4
gpt4 key购买 nike

下午好,

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

脚本的最终目标:有一个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

希望这可以帮助。

关于powershell - Powershell将所有ADuser组放入CSV的唯一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51735206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com