0

I am trying to create AD report in html via powershell but unable to fetch details from multiple domain,

below is my code,

# HTML Style
$a = "<style>"
$a = $a + "BODY{background-color:SkyBlue;font-family: calibri; font-size: 10pt;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: grey;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$a = $a + "</style>"


# Query Range
$dt = (get-date).adddays(-3)

# Domain Selection
$Domains = 'test1.test.com' , 'test2.testuat.com'

ForEach ($domain in $Domains) {

$report += get-aduser -Server $domain -filter 'whencreated -ge $dt' -Properties * | 

# Attributes selection
select whenCreated,
SamAccountName,
GivenName,
Surname,
DisplayName,
Description,
EmployeeID,
mail,
Office,
City,
Title,
Department,
Company,
ScriptPath,
@{name=”MemberOf”;expression={$_.memberof -join “;”}}
}
$report | convertto-html -head $a | Out-File C:\scripts\ad.html
Invoke-Expression C:\scripts\ad.html
4
  • What does "unable to fetch details" mean? You get an error or an empty list or what? What credentials are you using? Are the domains in the same forest? Please, provide some more details.
    – iPath
    Feb 13 '16 at 8:35
  • 1
    your $a variable is always rewritten at the top. don't always write $a = x, instead put it in a script block $a = { All HTML Text in here }, then on the foreach loop you write foreach ()) but it should be foreach(). in the get-aduser part you dont need to put the $domain in a scriptblock, and the filter has to be in singlequotes, not in a scriptblock. so your code would be get-aduser -server $domain -filter 'whencreated -ge $dt' -properites * | #other stuff clear this in your code, try it again and see if it works.
    – SimonS
    Feb 14 '16 at 12:50
  • @iPath hi, i am getting output for only one domain, and both the domains are in same forest.
    – Vino
    Feb 14 '16 at 15:41
  • @SimonS thanks for the suggestion, will test by tomorrow and let you know
    – Vino
    Feb 14 '16 at 15:44
1

Everytime this pipeline is executed:

convertto-html -head $a | Out-File C:\scripts\ad.html

you generate a whole new html document and the file (C:\scripts\ad.html) is being overwritten.

Replace the foreach(){} loop with the ForEach-Object cmdlet and move the |ConvertTo-Html |Out-File commands outside the loop:

$Domains |ForEach-Object {
  $Domain = $_
  Get-ADUser -Server $domain -Filter {whencreated -ge $dt} -Properties * | Select-Object whenCreated # and so on.
} |ConvertTo-Html -Head $a |Out-File C:\scripts\ad.html
2
  • -append parameter should do
    – strongline
    Feb 15 '16 at 2:55
  • Thanks @Mathias R. Jessen & strongline your suggestion were helpful
    – Vino
    Feb 17 '16 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.