Where I work, about half of the servers I manage are active directory member servers, and the other half are standalone servers. Eventually they will all be in AD, but for now, thats just the way things are. Anyway, it has obviously been a systems management problem for running remote scripts. Now that Powershell is finally out, I decided to try and write some scripts to make my life easier. I decided to start blogging about some of what I’ve done because it is hard to find sample Powershell scripts, and the books that are currently out don’t quite go in depth enough.
So this brings me to my first Powershell function called get-servers.
get-servers will search an Active Directory OU (or more) for all computer objects, store it in a hashtable. In addition, it reads a text file (in the current directory) and adds those servers to the same hashtable. Server names are stored as keys, and “ad” or “standalone” is stored as the value. This way, I can use simple if/else statements to determine whether or not I need to pass along alternate credentials when using cmdlets such as get-wmiobject.
function get-servers()
{
$ErrorActionPreference = "silentlycontinue"
$objComputer = @()
$strCategory = "computer"
$servers = @{}
# bind to the directory - this also sets our starting point for the search
$objDomain = [ADSI]'LDAP://OU=myou,DC=mydomain,DC=com'
$objDomainDC = [ADSI]'LDAP://OU=Domain Controllers,DC=mydomain,DC=com'
$objSearcher = new-object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")
[void]$objSearcher.PropertiesToLoad.Add("name")
# run the search
$colResults = $objSearcher.FindAll()
$objSearcher.SearchRoot = $objDomainDC
$colResults += $objSearcher.FindAll()
$colResults | foreach-object { $servers.$($_.properties.name) = "ad" }
# add standalone servers
get-content servers.txt | foreach { $servers.$_ = "standalone" }
return $servers
}
Edit and save this to a file such as “get-servers-function.ps1″ and load it into your current environment by running:
. ./get-servers-function.ps1
Then if you run get-servers then it should return your servers as designed. The advantage to having this in a function is so that you don’t have to duplicate the code in every script that you write. All you have to do when writing a new script is just add this line near the top:
$servers = get-servers
Enjoy!
1 comment:
Hi. :D
Post a Comment