【问题标题】:User & Group Audit Script用户和组审计脚本
【发布时间】:2015-10-06 04:18:31
【问题描述】:

我正在寻求帮助编写一个查询 Active Directory 并输出 CSV 的 powershell 脚本。

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

输出将如下所示: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

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您只需要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
    

    【讨论】:

    • 我很抱歉,我不是想冒犯。我花了很多时间尝试各种不同的方法,但这些方法不起作用,我一直在寻找一张白纸。我会用我迄今为止尝试过的内容更新我的帖子。
    • 你没有冒犯我,如果你没有提供证据证明至少尝试自己解决问题,我只是不会为你做这项工作。我将更新我的答案,使其比仅仅为您提供要使用的 cmdlet 更有帮助。
    • 效果很好。感谢您的帮助和耐心。谢谢。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    相关资源
    最近更新 更多