用户&小组审核脚本

时间:2015-10-01 21:47:00

标签: powershell active-directory

我正在寻找有关编写PowerShell脚本的帮助,该脚本将查询Active Directory并输出CSV。

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

输出如下所示:https://imgur.com/1MfFv7Q

我尝试过使用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上传和更多信息。再次感谢TheMadTechnician!

我的目标是从我的每个客户端获取此信息,使用带有时间戳的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

1 个答案:

答案 0 :(得分:2)

您所需要的只是Get-ADUserGet-ADGroupNew-ObjectAdd-MemberExport-CSV。我建立一个连接其可分辨名称和显示名称的组哈希表。然后,我获得所有用户的列表,为每个用户创建自定义对象,循环遍历组列表,并为每个组的自定义对象添加属性。然后遍历用户的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