Решено Access matrix AD -Матрица доступа пользователй AD

Simyrun

Почетный гость
Добрый день,
прошу помощи в исправлении ошибок скрипта.
Как это планировалось - пользователи занимают первый столбец а группы последующие. Маркируя пересечения данных строк и столбцов
Код:
#Suffix for weekly scan

$DateSuffix = Get-Date -Format "yyyy.MM.dd_HH.mm"
#Out file name
$OutFileName = "C:\Users\user\Documents\Access_Matrix_$DateSuffix.csv"
#OU to find PCs
$DomainUnit = 'OU=Users,OU=correct,OU=correct,OU=correct,DC=correct,DC=correct,DC=correct'
#Create table
$table = @()
foreach($user in Get-ADUser -Filter * -SearchBase $DomainUnit | select -First 10)
{
    $groups = Get-ADPrincipalGroupMembership -Identity $user
    $entry = New-Object PSObject
    $entry | Add-Member -Name "User Name" -MemberType NoteProperty -Value "$($user.Surname) $($user.GivenName)"
    $entry | Add-Member -Name "User Account" -MemberType NoteProperty -Value $($user.SamAccountName)
    foreach($group in $groups)
    {
        $entry | Add-Member -Name $group.Name -MemberType NoteProperty -Value "+"
    }

    $table += $entry
}
$table | Out-GridView
    $table | Export-Csv -NoTypeInformation -Path $OutFileName

Ошибка:
Get-ADPrincipalGroupMembership : An unspecified error has occurred

Есть другая версия того же самого, она работает и завершается без ошибок. Но там пользователи берутся только из первой группы и расположение их не верное


Код:
#Suffix for weekly scan

$DateSuffix = Get-Date -Format "yyyy.MM.dd_HH.mm"
#Out file name
$OutFileName = "C:\Users\user\Documents\Access_Matrix_$DateSuffix.csv"
#OU to find PCs
$DomainUnit = 'OU=Users,OU=correct,OU=correct,OU=correct,DC=correct,DC=correct,DC=correct'

#Create table
$table = @()
foreach($group in Get-ADGroup -Filter * -SearchBase $DomainUnit )
{
    $users = Get-ADGroupMember -Identity $group
    $entry = New-Object PSObject
    $entry | Add-Member -Name "Group Name" -MemberType NoteProperty -Value "$($group.Name)"

    foreach($user in $users)
    {

#        $entry | Add-Member -Name "$($user.Surname) $($user.GivenName) ($($user.SamAccountName))" -MemberType NoteProperty -Value "+"
        $entry | Add-Member -Name $user.SamAccountName -MemberType NoteProperty -Value "+"

    }
      
    $table += $entry

}

$table | Out-GridView
$table | Export-Csv -NoTypeInformation -Path $OutFileName
 
Последнее редактирование модератором:
Может быть кому то пригодится

Код:
#Suffix for weekly scan

$DateSuffix = Get-Date -Format "yyyy.MM.dd_HH.mm"

#Out file name

$OutFileName = "way to folder \file.scv"

#OU to find PCs

$DomainUnit ='OU with all user'

$Server = "domain controller"

$Filter = "Path_phrase"



#Create table

$table = @()



$groups = Get-ADGroup -Server:$Server -Filter * -Properties Name | Where-Object {$_.Name.ToString().ToUpper().Contains($Filter)}

$entry = New-Object PSObject

$entry | Add-Member -Name "User Name" -MemberType NoteProperty -Value ""

$entry | Add-Member -Name "User Account" -MemberType NoteProperty -Value ""

foreach($group in $groups) { $entry | Add-Member -Name $group.Name.Replace(" ", "_") -MemberType NoteProperty -Value "" }

             

$table += $entry



foreach($user in Get-ADUser -Filter * -SearchBase $DomainUnit -Properties MemberOf, Surname, GivenName, SamAccountName | select MemberOf, Surname, GivenName, SamAccountName)

{

    $groups = $user.MemberOf | Get-ADGroup -Server:$Server | Where-Object {$_.Name.ToString().ToUpper().Contains($Filter)}

    $entry = New-Object PSObject

    $entry | Add-Member -Name "User Name" -MemberType NoteProperty -Value "$($user.Surname) $($user.GivenName)"

    $entry | Add-Member -Name "User Account" -MemberType NoteProperty -Value $($user.SamAccountName)



    foreach($group in $groups)

    {

        $entry | Add-Member -Name $group.Name.Replace(" ", "_") -MemberType NoteProperty -Value "+"

    }

             

    $table += $entry

}



$table | Out-GridView

$table | Export-Csv -NoTypeInformation -Path $OutFileName
 
Назад
Верх