Solved

Powershell script to generate a report of all computers in OU

Posted on 2014-10-22
10
763 Views
Last Modified: 2014-10-29
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
0
Comment
Question by:Olevo
10 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40398469
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

0
 
LVL 1

Author Comment

by:Olevo
ID: 40398508
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
0
 
LVL 5

Expert Comment

by:Mai Ali (MVP)
ID: 40398990
For script that list all computers on ou, you can check below link
http://www.hofferle.com/generating-lists-of-computer-names-with-powershell/
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40399714
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

0
 
LVL 1

Author Comment

by:Olevo
ID: 40400663
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40400666
What is your internal website hosted with?
0
 
LVL 1

Author Comment

by:Olevo
ID: 40400812
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?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40400847
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.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40402199
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

0
 
LVL 1

Author Comment

by:Olevo
ID: 40412472
Thanks Joshua that is exactly what I want
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now