Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1326
  • Last Modified:

Powershell script to generate a report of all computers in OU

I need to create a PowerShell script which will generate a report for me of all computers in OU.

Report needs to be formatted as html or excel file and should look like this:

Name                OperatingSystem                                     Description
---------                --------------------------                                   -----------------
Server2             Windows Server 2008 R2 Standard     File Server
Lync13Srv        Windows Server 2008 R2 Standard      Lync 2013 Server
Server20          Windows Server 2012 R2 Standard      Standalone Hyper-V Server

Note: Report must have 'Description' field
Thanks in advance
1 Solution
David Johnson, CD, MVPOwnerCommented:
something along this line will work

$computers = Get-ADComputer -LDAPFilter "(name=*)" -SearchBase "CN=Computers,DC=Fabrikam,DC=com"
foreach ($computer in $computers)
Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer | Select-Object -Property * | select PSComputername, Name ,Description | export-csv c:\example.csv 

Open in new window

OlevoAuthor Commented:
Thanks David, my PowerShell skills is not up to the speed just yet. Please correct me if I wrong. First you get the list of all computers (Get-ADComputer) in OU and then you‘re querying (connecting to) computer with WMI command (Get-WmiObject) to get additional info. I’m I right? If this the case we will have problem getting info from the computers which are not online (switched off). All I want is to get info from Domain Controller… Perhaps, we can get ‘Description’ info from ‘Attribute Editor’
Attribute Editor
Mai Ali (MVP)Senior Infrastructure ConsultantCommented:
For script that list all computers on ou, you can check below link
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Joshua GrantomSenior EngineerCommented:
Here is a 1 liner

Get-ADComputer -filter * -SearchBase "OU=Servers,DC=Fabrikam,DC=com" -properties Name,OperatingSystem,Description | Select Name,OperatingSystem,Description | Export-CSV C:\ServerInfo.csv -nti

Open in new window

OlevoAuthor Commented:
Thanks Joshua, that is exactly what I want. May I trouble you for an additional request? How hard it would be to make something like this: Our internal web site will have a page with a link, when I click on it, code within a web page (PowerShell command above)  will generate Servers information (Name,OS, Description, etc.) as a nice formatted html table.
Joshua GrantomSenior EngineerCommented:
What is your internal website hosted with?
OlevoAuthor Commented:
Thanks Joshua, This "Company Servers information" web page will be running on IIS 8.5 (Windows Server 2012 R2). Basically I need this page for my manager, so he can see list (click on a link) all our servers and what they do (Description) in our AD domain any time he wants. I'm guessing that it would be much easier just to run PowerShell script as a schedule task instead of fulling around with the web development?! Anyway, what do you think how I should accomplish my goal?
Joshua GrantomSenior EngineerCommented:
Tomorrow I will modify the powershell script to output to an HTML document, then you can just have an HTML link to the output file.

Then you can run the powershell as a scheduled task and it will update the html file on whatever schedule you want. Simple as that.
Joshua GrantomSenior EngineerCommented:
Here you go! This has basic styling but you can modify that how you like if you want to add color or adjust spacing.

#Page Title
$title = "Server Information"
#HEAD Section (CSS for Styling)
$head = "<style> BODY{background-color:#FFFFFF;} TABLE{width: 75%; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;background-color:#999999} TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#FFFFFF} </style>"
#Page Header
$body = "<H2>Information about Servers in $search</H2>"
#Path to save html report
$path2output = "C:\ServerInfo.html"
#OU to query for report
$search = "OU=Servers,DC=DOMAIN,DC=com"

Get-ADComputer -filter * -SearchBase $search -properties Name,OperatingSystem,Description | ConvertTo-Html Name,OperatingSystem,Description -title $title -head $head -body $body | Set-Content $path2output

Open in new window

OlevoAuthor Commented:
Thanks Joshua that is exactly what I want

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now