0

I'm creating a script that tells me the creation / modification date and other pieces of info of AD objects to determine upgrade status of the machines in large domains. I have no problem accomplishing this in a well formatted and easy to read manner in Server 2008 because it has Active Directory modules, but this isn't the case with Server 2003.

With server 2003 I had to use a different approach to the script to gather the information I want, but I am unsure how to format this.

This is my current script:

$filePath = “$ENV:UserProfile\Desktop\output.txt”
## Enter the name of the Domain controller below
$DCName = “Exchange”
$computers = dsquery computer domainroot -name * -limit 0

Foreach ($computer in $computers) {
                repadmin /showattr $DCName $computer /atts:"WhenChanged,WhenCreated,OperatingSystem" /long | out-file –append $filepath
                }

This is the sample output:

DN: CN=Sample-Object,CN=Computers,DC=Contoso,DC=com

    1> whenCreated: 07/04/2011 14:00:02 Pacific Standard Time Pacific Daylight Time
    1> whenChanged: 08/09/2012 11:24:22 Pacific Standard Time Pacific Daylight Time
    1> operatingSystem: Windows 7 Professional

In server 2008 I'm able to use string formatting ('"{0}","{1}"' -F $computer.name, $computer.whatever) amd output it to a .csv to make it presentable but I don't think the same methods will apply to the results of repadmin.

My end goal would to simply have a CSV with Computer Name, along with the three or however many attributes I have extracted from repadmin.

Any help appreciated, thank you.

smierdziel
  • 133
  • 2
  • 3
  • 9

1 Answers1

0

Give this a try, you can export it to CSV and import it back as objects:

$result = dsquery computer domainroot -name * -limit 0 | foreach {
    repadmin /showattr $DCName $_ /atts:"WhenChanged,WhenCreated,OperatingSystem" /long
} | Out-String

$result.split([string[]]'DN:',[StringSplitOptions]::RemoveEmptyEntries) | Foreach-Object{

    $attr = $_.Split("`r`n",[StringSplitOptions]::RemoveEmptyEntries)

    New-Object -TypeName PSObject -Property @{
        DN = $attr[0].Trim()
        whenCreated = $attr[1].Trim() -replace '^1> whenCreated: '
        whenChanged = $attr[2].Trim() -replace '^1> whenChanged: '
        operatingSystem = $attr[3].Trim() -replace '^1> operatingSystem: '
    }

}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • This worked out well, thank you! I have some follow up questions of you would be so kind. Would there be any way to filter by OU? It doesn't appear as if dsquery / repadmin have that capability but I tried using the where-object statement at the end of the first lines. `$result = dsquery computer domainroot -name * -limit 0 | foreach { repadmin /showattr $DCName $_ /atts:"WhenChanged,WhenCreated,OperatingSystem" /long } | Where-Object {$_ -notlike "*OU=Disabled Computers*"}` However, this just appears to erase the first line, leaving the rest of the repadmin query for that object. – smierdziel Sep 07 '12 at 20:54
  • Add a where clause after the first split. Replace line 5 with this: $result.Split([string[]]'DN:',[StringSplitOptions]::RemoveEmptyEntries) | Where-Object {$_ -notlike '*OU=Disabled Computers*'} | Foreach-Object {... – Shay Levy Sep 08 '12 at 08:49