Friday, February 24, 2012

I was interrupted in my script that I was working on yesterday, so maybe I'll get back to that this weekend.

The interruption today was to enumerate all of the servers in a particular OU that is going to have a Group Policy change coming up. We need to document all of our changes in a Change Management application, and one of the required fields is 'Affected Systems'.  I figured the easiest way to get a text list of the servers affected would be to run a Powershell script, and it took all of about 30 seconds to find something that would work.

http://social.technet.microsoft.com/Forums/en-US/winserverManagement/thread/a0cebabf-3fc9-49b0-be55-5e4ff3232b7b/

The code I used wasn't from the correct answer, but from this guy's answer:


$strComputers = @()
$ou = [ADSI]"http://www.blogger.com/ EB,DC=EB-MILLIMAN,DC=COM"
foreach ($child in $ou.psbase.Children) {
 if ($child.ObjectCategory -like '*computer*') { $strComputers += $child.Name }
}


This code puts the Computers into the string $strComputers, which in turn can be called...

I simply modified the code to reflect my own AD structure and then did a write-host $strComputers.

P.S.  My first professional job out of college was as a consultant with Milliman & Robertson (now Milliman, Inc.).  Whoever posted the code should be advised not to give away too much information about their internal AD structure on the Internet.


Update:  I've found that using variable names with Hungarian Notation ($strComputers) is not a best practice for Powershell.  http://www.powershellcommunity.org/Wikis/BestPractices/tabid/79/topic/Naming/Default.aspx

 

No comments:

Post a Comment