[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

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
0
Justin Owens
Asked:
Justin Owens
  • 5
  • 4
  • 3
  • +2
4 Solutions
 
pjamCommented:
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
0
 
KenMcFCommented:
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

0
 
rlandquistCommented:
Here is an expansion on KenMcF's answer.
This will export to csv to be opened in Excel
get-qadcomputer| where-object {$_.OSName -like "*server*"}|select-object computername, osname,
osservicepack | export-csv C:\temp\serverlist.csv -notype

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rlandquistCommented:
Here is the link to download the Quest AD cmdlets:
http://www.quest.com/powershell/activeroles-server.aspx
0
 
Justin OwensITIL Problem ManagerAuthor Commented:
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
0
 
KenMcFCommented:
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
0
 
Justin OwensITIL Problem ManagerAuthor Commented:
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
0
 
KenMcFCommented:
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



0
 
Justin OwensITIL Problem ManagerAuthor Commented:
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
0
 
Chris DentPowerShell DeveloperCommented:
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
0
 
Chris DentPowerShell DeveloperCommented:
Just in case, this is how I'd wrap it up.
$Servers | Where-Object { Test-Connection $_.ComputerName -Count 1 -Quiet } |
  ForEach-Object {
    Get-WmiObject Win32_Service -Computer $_.ComputerName | 
      Where-Object { 
        ($_.startname -ne "LocalSystem") -And
        ($_.Startname -ne "NT AUTHORITY\LocalService") -And 
        ($_.startname -ne "NT AUTHORITY\NetworkService") } |
      Select __Server, Name, StartName, @{n='IPAddresses';e={ ([Net.Dns]::GetHostEntry($_.__SERVER)).AddressList }} } |
  Export-Csv d:\ServicesExport.csv

Open in new window

Untested, but fingers crossed :)

Chris
0
 
Justin OwensITIL Problem ManagerAuthor Commented:
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
0
 
KenMcFCommented:
Sorry for the delay and Thanks Chris for cleaning it up a little, always like your input. Here is an update with everything together. I have also added a line to get the current user. This will only get a user that is logged into the console of that server. Depending on how many computers you have you also may need to add -sizelimit 0 after get-qadcomputer.

get-qadcomputer -sizelimit 0


get-qadcomputer | where-object {$_.OSName -like "*server*"} | 
    Where-Object { Test-Connection $_.Name -Count 1 -Quiet } |  
      ForEach-Object {  
        Get-WmiObject Win32_Service -Computer $_.Name |   
          Where-Object {   
            ($_.startname -ne "LocalSystem") -And  
            ($_.Startname -ne "NT AUTHORITY\LocalService") -And   
            ($_.startname -ne "NT AUTHORITY\NetworkService") } |  
                Select __Server, Name, StartName, 
                @{n='IPAddresses';e={ ([Net.Dns]::GetHostEntry($_.__SERVER)).AddressList }},
                @{n='Logged On User';e={ (get-wmiobject -computer $_ -class win32_computersystem).Username}} } | 
                    Export-Csv d:\ServicesExport.csv -notype

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:
Inclusion in the first report is simple enough, just a case of adding the same custom property as we have above:
Get-QADComputer -OperatingSystem "*Server*" | Select-Object Name, OSName, OSServicePack, ComputerRole,
    @{n='IPAddresses';e={ ([Net.Dns]::GetHostEntry($_.Name)).AddressList }} | 
  Export-Csv C:\users\********\desktop\serverlist.csv -NoTypeInformation

Open in new window

Chris
0
 
Justin OwensITIL Problem ManagerAuthor Commented:
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
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now