segunda-feira, 28 de outubro de 2013

Criando um relatório com membros de grupos do AD em html usando powershell

Segue um exemplo de como criar um relatório em html usando powershell do usuário membros de grupos dentro de um determinada OU.
Esse script pode ser adaptado para consultar grupos com diversas condições.


import-module activedirectory

# Aqui pode-se limitar os grupos a uma OU ou ao dominio inteiro
$OU = 'OU=Grupos_Permissoes_Pastas'

#Tambem pode-se customizar essa pesquisa
$Groups = Get-ADGroup -Filter '*' | select-object * | where-object {$_.distinguishedname -like "*,$OU,*"} | Select-Object Name

#Groups = Get-ADGroup -filter {Name -like $GroupName} | Select-Object Name#

$fileName = "GroupReport.html" 
New-Item -ItemType file $fileName -Force 
 
# Codigo HTML 
Add-Content $fileName "<html>" 
Add-Content $fileName "<head>" 
add-content $fileName '<STYLE TYPE="text/css">' 
add-content $fileName  "<!--" 
add-content $fileName  "td {" 
add-content $fileName  "font-family: Tahoma;" 
add-content $fileName  "font-size: 11px;" 
add-content $fileName  "border-top: 1px solid #999999;" 
add-content $fileName  "border-right: 1px solid #999999;" 
add-content $fileName  "border-bottom: 1px solid #999999;" 
add-content $fileName  "border-left: 1px solid #999999;" 
add-content $fileName  "padding-top: 0px;" 
add-content $fileName  "padding-right: 0px;" 
add-content $fileName  "padding-bottom: 0px;" 
add-content $fileName  "padding-left: 0px;" 
add-content $fileName  "}" 
add-content $fileName  "body {" 
add-content $fileName  "margin-left: 5px;" 
add-content $fileName  "margin-top: 5px;" 
add-content $fileName  "margin-right: 0px;" 
add-content $fileName  "margin-bottom: 10px;" 
add-content $fileName  "" 
add-content $fileName  "table {" 
add-content $fileName  "border: thin solid #000000;" 
add-content $fileName  "}" 
add-content $fileName  "-->" 
add-content $fileName  "</style>" 
Add-Content $fileName "</head>" 
Add-Content $fileName "<body>" 
Add-Content $fileName "<table width='100%'>" 
Add-Content $fileName "<tr bgcolor='#0000FF'>" 
Add-Content $fileName ("<td width='35%'><font size=6>Grupo do Active Directory e seus membros na " + $OU + "</font></td>" )
Add-Content $fileName "</tr>" 
 
$alternateTableRowBackground = 0

ForEach ($Group in $Groups)
   {
   Add-Content $fileName "<tr bgcolor='#FFD700'>"
   Add-Content $fileName ("<td width='30%'>" + $group.name + "</td>")
   Add-Content $fileName "<tr bgcolor='#FCFCFC'>"
   $membros = Get-ADGroupMember -identity $($group.name) -recursive | Select-Object samaccountname,Name
   Add-Content $fileName ("<td width='30%'>")
        ForEach ($member in $membros)
            {
                Add-Content $fileName ( $member.Name + "->" + $member.samaccountname + "</br>")
            }
   Add-Content $fileName ("</td>") 
   Add-Content $fileName "</tr>"
 
   }

Add-Content $fileName "</table>" 
 
Add-Content $fileName "</body>" 
 
Add-Content $fileName "</html>"