Question

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.

Was it helpful?

Solution

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: '
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top