Link to home
Start Free TrialLog in
Avatar of Justin Owens
Justin OwensFlag for United States of America

asked on

Need Script to Poll AD for Servers

Experts,

I have taken over as the System Administrator at a new company (at least new to me).  There is not any documentation of any kind.  I would like a script which will export to a spreadsheet (CSV is fine):

1) Poll AD for all Computer Objects which are any Server OS (I don't care about workstations)
2) List them by computer name
3) List the OS (and patch level, if possible)
4) Get the current IP (either from DNS or by some other method)
5) Get the last logged on user / current logged on user (and distinguish between the two)
6) List any Services running on the server which use a Domain account (such as BESAdmin on the BES related services)

I don't care if the solution is one or multiple scripts.  I don't care what language it/they is/are in.  I am sure that all of this has been done before, but rather than re-invent the wheel, I thought I would ask a bunch of folks smarter than me.

Cheers,

DrUltima
Avatar of pjam
pjam
Flag of United States of America image

Have you visited the Technet Script Center?  that is where I would look.  Pretty strange that you have No documentation, did previous person destroy?
Lots of PS there.
http://gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=activedirectory&f%5B0%5D.Text=Active%20Directory&f%5B1%5D.Type=SubCategory&f%5B1%5D.Value=computeraccounts&f%5B1%5D.Text=Computer%20Accounts
I would get the Quest AD cmdlets and use powershell for most of this. Here are some examples.

get-qadcomputer | Where {$_.osname -match "Server"} | Select Name, OSVersion, OSSerivcePack


$Servers = get-content c:\servers.txt
$Servers | Foreach {gwmi -computer $_ -class win32_service | Where {
    ($_.startname -ne "LocalSystem") -And ($_.Startname -ne "NT AUTHORITY\LocalService") -And ($_.startname -ne "NT AUTHORITY\NetworkService")}} | 
        Select __Server, Name, StartName

Open in new window

SOLUTION
Avatar of rlandquist
rlandquist
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the link to download the Quest AD cmdlets:
http://www.quest.com/powershell/activeroles-server.aspx
Avatar of Justin Owens

ASKER

Experts,

I was able to use the line posted in http:#a35514409 to get an enumerated list.  When using the code in post http:#a35514190 I get this error for each line in the CSV:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\*********\Desktop\serverservices.ps1:2 char:25
+ $Servers | Foreach {gwmi <<<<  -computer $_ -class win32_service | Where {
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Open in new window


Further guidance is appreciated.

DrUltima
If you are importing a CSV file try this with the rest of the code posted before.

$Servers = import-csv c:\servers.csv
$Servers | Foreach {gwmi -computer $_.Computername
KenMcF,

That made the script run, but it didn't error check (if the server is in AD but not live, it throws an error).  I also noticed that on successful polls, it throws the data to the console.  Here is what I used:

$Servers = import-csv c:\users\*******\desktop\serverlist.csv
$Servers | Foreach {gwmi -computer $_.Computername -class win32_service | Where {
    ($_.startname -ne "LocalSystem") -And ($_.Startname -ne "NT AUTHORITY\LocalService") -And ($_.startname -ne "NT AUTHORITY\Network Service")}} | Select __Server, Name, StartName

Open in new window


Admittedly, I don't know nearly enough about PowerShell, but it seems to export it, I would need something like this:

$Servers = import-csv c:\users\********\desktop\serverlist.csv
$Servers | Foreach {gwmi -computer $_.Computername -class win32_service | Where {
    ($_.startname -ne "LocalSystem") -And ($_.Startname -ne "NT AUTHORITY\LocalService") -And ($_.startname -ne "NT AUTHORITY\NetworkService")}} | Select __Server, Name, StartName | Export-Csv c:\users\********\desktop\ServicesExport.csv

Open in new window


I also have no idea how to make it do a "If you can't find the server, then just log it as unavailable" type of entry.

DrUltima
Here is one that will test to see if the host is up before it does the WMI query. I can post more tonight when I have more time to output if the server was not avalible.

 
$Servers | Foreach {
If (Test-Connection $_.ComputerName -count 1 -q){
gwmi -computer $_.ComputerName -class win32_service | Where {  
    ($_.startname -ne "LocalSystem") -And ($_.Startname -ne "NT AUTHORITY\LocalService") -And ($_.startname -ne "NT AUTHORITY\NetworkService")}}} | 
        Select __Server, Name, StartName | 
            Export-Csv d:\ServicesExport.csv

Open in new window



KenMcF,

Thank you again.  I look forward to your further assistance.  The last thing I would like to do is determine (by polling DNS or the computer itself) what the IP address should be.

DrUltima
Avatar of Chris Dent
The Native (.NET) way is:

[Net.Dns]::GetHostEntry("somehostname")

There are non-native ways, but this is the simplest :) Pull the AddressList property off any response you get. I'd chuck that into Select-Object myself.

Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris,

Thanks for jumping in... It works as you have placed it (once I changed file names and locations).  Unfortunately, I only get entries for servers who have domain-used services doing it this way.  It would be better if I can get the IP address in the first report, which is generated with this single line:

 
get-qadcomputer| where-object {$_.OSName -like "*server*"}|select-object computername, osname, osservicepack, computerrole | export-csv C:\users\********\desktop\serverlist.csv -notype

Open in new window


DrU
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Experts,

Thank you for your diligence and quick responses.  I was able to obtain working PowerShell scripts which generated the exact reports I needed.

DrUltima