monitor

One place the powershell shines is in monitoring.  With full access to WMI and CMI cmdlets as well as the .NET tools, and with remoting or with -computername as a local option, it is reasonably easy to run monitors over a list of servers and report back characteristics, such a version, patches, last reboot time, et al.

Obviously we have more than one server.  Well, i guess it is obvious – just look at my falling arches and declining hair.  This makes a foreach (or similar construction typical.

So – make an array

Servers = "server1","server2","server3"...,"lastserver"

and put it directly in the program/script.  You can do the same thing, but with a little more flair and formatting by using a here-string or a list with continuations (note the comma and the backtick. Some servers are commmented out – not a problem unless it is the last server in the list.

$servers = (
"servercluster-sql.university.edu",`
 "servercluster1.university.edu",`
 "servercluster2.university.edu",`
...
 "server4.university.edu",`
# "server5.university.edu",`
 "server6university.edu",`
# "lastservern.university.edu",`
 "lastserver.university.edu"
)

Either of these puts the list into the variable $servers and you can iterate over them.

Similary, we can put the list of servers into a txt file in a central location (even a UNC path to a file share), and read it in with get-content.  Then foreach over the variable. Y0u can put the gc right in the middle of the foreach loop to economize space.

But we have lots (no, they are not all mine) of servers, and we have Active Directory with a servers OU in it.  How about reading directly from it?  (Assuming it is maintained properly)

Do this:

$servers = @()
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://adc.university.edu/ ou=servers, DC=university,DC=edu")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
 {
 $objUser = $objResult.GetDirectoryEntry()
 $server = ($objuser.sAMAccountName).ToString()
 $servers += $server.replace("$","")
 }

The sAMAccountName i got back had a $ at the end of each name.  I have no idea… i just removed it.  The servers are now all stored in the $servers array for further use.

About that maintenance task – it appears that some machines have been decommissioned and not removed from the OU.  Or in one case, the machine name was listed like the movie title:  server-GHOSBUSTERS.  Ok i could figure that one out myself, but in other cases, i decided to test for presence using ping.  (or more correctly test-connection).

try {
write-output "$server"
  if (Test-Connection -CN $server -Count 1 -BufferSize 16 –Quiet){
     # do server found stuff inside the script block
  } else {
     write-output "$server not found by ping"
  } # end of test connection logic
}  # end of try
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.