Powershell 用户及;组审核脚本

Powershell 用户及;组审核脚本,powershell,active-directory,Powershell,Active Directory,我正在寻找帮助编写一个powershell脚本,将查询Active Directory并输出CSV 此脚本将列出所有组和所有用户,并在用户属于该组时用字符表示 输出如下所示: 我尝试过使用dsquery和其他各种powershell方法,但似乎都不起作用 我希望这里的人对这件事有不同的看法,并能提供帮助 谢谢大家! 更新1: 根据要求,这是我以前尝试使用的代码 #Get a list of the groups $groups = Get-ADGroup -filter * -Propertie

我正在寻找帮助编写一个powershell脚本,将查询Active Directory并输出CSV

此脚本将列出所有组和所有用户,并在用户属于该组时用字符表示

输出如下所示:

我尝试过使用dsquery和其他各种powershell方法,但似乎都不起作用

我希望这里的人对这件事有不同的看法,并能提供帮助

谢谢大家!

更新1: 根据要求,这是我以前尝试使用的代码

#Get a list of the groups
$groups = Get-ADGroup -filter * -Properties Name | Select Name

#iterate through groups array and append each with a comma
$output = ForEach ($g in $groups){
$topgroups.Add($g)
$topgroups.Add(",")
}

#for each group, find out if the user is part of that group
$output = ForEach ($g in $groups) {
 $results = Get-ADGroupMember -Identity $g.name -Recursive | Get-ADUser -Properties enabled, SamAccountName, givenname, surname,physicalDeliveryOfficeName

ForEach ($r in $results){
 New-Object PSObject -Property @{
    GroupName = $g.Name
    Username = $r.name
    DisplayName = $r.displayname
  }
 }
} 
$output | Export-Csv -path c:\temp\output.csv -NoTypeInformation
更新2:

添加了FTP上传和一些更多信息。再次感谢技术员

我的目标是从我的每个客户机获取这些信息,将其导入带有时间戳的SSIS的SQL,然后通过SQL报告进行比较

以下是我的脚本当前所在的位置:

New-Item c:\temp\audit -type directory

$Domain = (gwmi WIN32_ComputerSystem).Domain
$filename = $Domain + "_ADExport.csv"
$fileoutput = "c:\temp\audit\" + $filename

Remove-Item $fileoutput

$GroupRef = @{}
Get-ADGroup -filter * | ForEach{$GroupRef.Add($_.DistinguishedName,$_.Name)}

$Users = Get-ADUser -Filter * -Prop MemberOf, passwordlastset, LastLogonDate
ForEach($User in $Users){
 $LineItem = [PSCustomObject]@{'Enabled'=$User.Enabled;'First Name'=$User.givenname;'Last Name'=$User.surname;'Location'=$User.physicalDeliveryOfficeName;'Domain'=$Domain;'SAMAccountName'=$User.samaccountname;'LastLoggedOn'=$User.lastlogonDate;'PasswordLastSet'=$User.passwordlastset}
    $GroupRef.Values | ForEach{Add-Member -InputObject $LineItem -NotePropertyName $_ -NotePropertyValue ""}
    $User.MemberOf | ForEach{$LineItem.$($GroupRef["$_"]) = "X"}
[Array]$Results += $LineItem
}
$Results|export-csv $fileoutput -notype


#we specify the directory where all files that we want to upload  
$Dir="C:/temp/audit/"

#ftp server 
$ftp = "ftp://8.8.8.8/"
$user = "test" 
$pass = "ThisIsARea11yL0NgPa33Word"  

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

#list every file
foreach($item in (dir $Dir "*.csv")){ 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName) 
 }
更新3:

下午好:

我遇到了一个问题,我试图限制此搜索通过的OU:

$GroupRef = @{}
$OUPATH = (Get-ADOrganizationalUnit -Filter 'Name -like "CLIENT_GROUPS"' | FT DistinguishedName -HideTableHeaders | Out-String).Trim()
Get-ADGroup -SearchBase "$OUPATH" -Filter * | ForEach{$GroupRef.Add($_.DistinguishedName,$_.Name)}
错误是:

Exception setting "": "Cannot process argument because the value of argument "name" is not valid. Change the value of
the "name" argument and run the operation again."
At C:\Users\f12admin\Desktop\test.ps1:23 char:42
+     $User.MemberOf | ForEach{$LineItem.$($GroupRef["$_"]) = "X"}
+                                          ~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting

您只需
获取ADUser
获取ADGroup
新建对象
添加成员
,以及
导出CSV
。我会建立一个组的哈希表,将它们的distrignedname和displayname链接起来。然后我会得到一个所有用户的列表,为每个用户创建一个自定义对象,遍历组列表,并为每个组的自定义对象添加一个属性。然后遍历用户的MemberOf属性,并将自定义对象上的关联属性设置为“X”,以表示其中的所有内容。收集阵列中的所有自定义对象,并将其导出到csv

这没有经过测试,但这是理论

$GroupRef = @{}
Get-ADGroup -filter * | ForEach{$GroupRef.Add($_.DistinguishedName,$_.Name)}
$Users = Get-ADUser -Filter * -Prop MemberOf
ForEach($User in $Users){
    $LineItem = [PSCustomObject]@{'DisplayName'=$User.DisplayName;'SAMAccountName'=$User.samaccountname}
    $GroupRef.Values | ForEach{Add-Member -InputObject $LineItem -NotePropertyName $_ -NotePropertyValue ""}
    $User.MemberOf | ForEach{$LineItem.$($GroupRef["$_"]) = "X"}
    [Array]$Results += $LineItem
}
$Results|export-csv c:\temp\output.csv -notype

我道歉,我不是想冒犯你。我花了很多时间尝试各种不同的方法,但都不管用,我一直在寻找一张白纸。我会用我迄今为止所做的尝试更新我的帖子。你没有冒犯我,如果你没有提供证据证明至少你自己试图解决这个问题,我就不会为你做这项工作。我将更新我的答案,使其比提供要使用的cmdlet更有帮助。这非常有效。我感谢你的帮助和耐心。非常感谢。