Why I no longer hate writing documentation…

By Jesse Hamrick • April 21st, 2009

…it’s because I no longer have to! And you too will no longer document your computers and servers either. Why? Because PowerShell is going to do it for you and I’m going to share with you how I did it. Yes, it is going to be presented well, in Excel, and the Boss is going to think that you are the greatest thing since sliced bread.

Computer Inventory

I picked up a side job at the beginning of the year documenting the infrastructure for a company that will remain anonymous at this time. It doesn’t surprise me that many companies still do not maintain up-to-date documentation if any.  What is the “BIG DEAL” about documenting a system, is it really a “time factor” problem or “lack of resources” to do the job? I, like many of my peers, just think documenting SUCKS!!! So I set out to make my life and hopefully yours a little easier.

The Computer Inventory Script enumerates Hardware, OS, System, Processor, Memory, Disk, and Network information and neatly organizes it in an Excel file. I’ve provided Version 2 of the script that allows you to choose a number of computer resources; all computers in the domain, all servers in the domain, computers from a list maintained in a text file, and the ability manually pick a computer. Version 2 of the PowerShell script also allows you to send credential information should you wish to logon the remote computer with an alternative user account. This is helpful for DMZ servers as well as stand alone servers that are not members of your domain.

Please feel free to customize this script to fit your environment. I’m not going into any detail about the script as I am reserving that information for a new PowerShell tutorial that I am writing about COM objects. So check the tutorial section to for more information.

Download Script file here.

Here is the code so that you can view it before downloading. I would suggest downloading this file as format issues with copying the code below may occur.

View Script code here.

Email This Post To A Friend Email This Post To A Friend

Comments

Wow! What a great script! Just ran it for the first time. The way the results are capture in Excel is impressive.

Thanks, this helps a lot!!!

By Jacob Saaby on April 23rd, 2009 at 5:52 am

Love it. Unfortunately I can’t get it to work with Office 2007.

It just starts Excel, doesn’t create a sheet with information.

By Jesse Hamrick on April 23rd, 2009 at 8:43 am

For the record I am using Office 2003. I’ll get a copy of Excel 2007 and test the code…

By Jesse Hamrick on April 23rd, 2009 at 10:30 am

Tested script with Excel 2007 and it works. It may be the option you are choosing. If you are choosing the option to gather info from “All Computers” or “All Servers” in your domain, it may take a long time to enumerate all the information. Try Option [4]Choose a Computer manually to test enumerating from a single computer.

DUDE!!! This is the best! The memory sheet is my favorite love that I can see the number of slots, what size RAM is installed, and which slots are empty. Makes memory upgrades much easier without having to power down and opening the box.

By Jacob Saaby on April 23rd, 2009 at 11:50 pm

Hmm ok, that’s what I tried. Will make it run a bit longer and see.

By Henrik Huhtinen on May 6th, 2009 at 10:59 pm

The problem with empty Excel is with the current locale. Set current locale to “en-US” and it will work.

Would it be possible to add a software installed tab.

Hi there..

I been trying to find the Set current locale to “en-US” and tried replacing the code however no luck and help would be appreciated

#New Excel Application
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True

with

$ci = new-object globalization.cultureinfo en-US
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel.UserControl= $True
$Sheet = $Excel.Workbooks
$Sheet.PSBase.GetType().InvokeMember(“Add”,
[reflection.bindingflags]::InvokeMethod,$null, $Sheet, $null, $ci)

By Jamey Westmoreland on July 17th, 2009 at 7:06 am

I added this function
# ========================================================================
# Function Name Select-FileDialog #
# Created by Hugo Peeters #
# http://www.peetersonline.nl #
###############################

# Example use:
# $file = Select-FileDialog -Title “Select a file” -Directory “D:\scripts” -Filter “Powershell Scripts|(*.ps1)”

function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter=”All Files (*.*)|*.*”)
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq “OK”)
{
Return $objForm.FileName
}
Else
{
Write-Error “Operation cancelled by user.”
}
}

# ==============================================================================================

And changed the line
$strText = Read-Host “Enter the path for the text file”

To
$strText = Select-FileDialog -Title “Select a file” -Directory “c:” -Filter “Text Files (*.txt)|*.txt”

now I can browse to select the text file of computers.

I get an error when excel opens too..

Which computer resources would you like in the report?

Exception calling “Add” with “0″ argument(s): “Old format or invalid type library. (Exception from HRESULT: 0×80028018 (TYPE_E_INVDATAREAD))”
At :line:390 char:29
+ $Excel = $Excel.Workbooks.Add <<<< ()

can someone send me final version of this script, I cannot configure it to run with excel 2007.
matiss(dot)baltalksnis@gmail.com

This script is great but i am new to powershell and need the uptime of the servers added to this script, how to do that????

Hi, How do I change this script to output to notepad, we do not have Excel?
Thanks

Can someone recomend a good book on PowerShell, for a beginner

This looks great – however when choosing option 1 for all domain computers only one machine shows up. No errors pop up but in the debug window I see the following:

“Which computer resources would you like in the report?”
Undetermined
Undetermined

Any help would be appreciated.

Thanks,

Brian

I’m a beginner when it comes to scripting and likewise in Powershell.
I would love to get this script working but i have only ended up with a blank excel, not even a worksheet.
I have tried it in two setups.
In both setups I ran it on local machine.

1.Vista Business SP2 Office 2010 Beta
2.XP SP3 Office 2003
Vista is fysical, XP is virtual(would have liked it to be the other way around).

I checked to see if all COM and WMI services were running – ok.

In Vista I allowed powershell in Firewall.
In XP I disabled firewall.

In Office2010 Beta i set folder, and subfolder of powershell-exe to be trusted.

Starting to suspect it could relate to some module missing in powershell or something else missing.

I have complete faith this is a wonderful script. But since you guru’s have been working with scrips so long I suspect this script does not work since I and other don’t meet the pre-requisites.
Is there anyway to find out pre-requisites, dependencies..

Thank you for shareing all you’re knowledge

Best Regards
/Bengt

I forgot to mention that i tried Jamey Westmoreland textversion provided in this thread. It resulted in me being posted to save the textfile numerous times as specified in the script but there were never any textfiles saved.
I also tried editing the script to use OpenOffice Calc instead (it’s installed next to Office 2010 Beta on my Vista-machine) but I was never able to “wake” Calc through the script.

Regards
/Bengt

To optimize the scan, I added a check to skip non-responding computers (e.g. linux machines, computers turned off, and unupdated AD records). Scanning of our network is a lot faster now.

Function WMILookup {
foreach ($StrComputer in $colComputers){
$GenItems1 = gwmi Win32_ComputerSystem -Comp $StrComputer
***********
if(!$GenItems1){
Write-Host “$StrComputer not found”
continue
}
***********

Thanks for the script! I also love your tutorials, especially the real-world examples. Very well written!

Menno

using powershell 2.
created a simple script to output the computer names and descriptions.
Trying to output from a specific OU to a spreadsheet and format the two fields: name and description with no luck so far.
here’s the script:
$strFilter = “computer”

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = “LDAP://servername/OU=laptops,ou=city,DC=domain,DC=com”
$objSearcher.SearchScope = “subTree”
$objSearcher.PageSize = 1000

$objSearcher.Filter = “(objectCategory=$strFilter)”
$colResults = $objSearcher.FindAll()

foreach ($i in $colResults)
{
$objComputer = $i.GetDirectoryEntry()
Get-WMIObject Win32_BIOS -computername $objComputer.Name,$objComputer.Description
}

Help would be appreciated. thanks.

Hi, in Excel 2010 it works only if add:
$culture = [System.Globalization.CultureInfo]“en-US”
$oldCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture

just before Excel calling.
Thanks for script

I tried to run this script but it will open a blank excel 20007 sheet. Could you please update me if i am doing anything wrong? I have not modified anything within the script.

Can another sheet be added so it inventory software that is on the local hard drive?

software inventory would be nice…

This is a sample code that lists inventory software I found..

Only thing is integrate this into excel file..

$RegPath= “SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”
$BaseKey =[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(“LocalMachine”, $ComputerName)
$OpenSubKey = $BaseKey.OpenSubKey($RegPath)

$file = localhost
foreach ($obj in $file) {

$OpenSubKey.GetSubKeyNames() | foreach {
$BaseKey.OpenSubKey(“$RegPath\$_”).GetValue(“DisplayName”)
}
}

Bman: Best guess is the line with the word Undetermined should actually read:
default{$Sheet5.Cells.Item($intRowDisk, 2)=”Undetermined”}

Menno: Thanks for the optimization!

Brilliant, just brilliant…

would it be possible for the ip address to be imported in the columns next to each other instead of the row below it? If so how would i do that.

Love the script! it has saved me weeks of work

This script is great. Added the code needed to drive the progress bar properly.

Way to go! Just ran the script on a Win7 SP1 desktop to a WinXP SP3 box and was floored by the process. A little more testing and I’ll try it on my servers.

I’m not sure if you’re still working on this, but I’m unable to run the script against my local PC (Win7 SP1). I’ll have to try it out on an XP machine.

Thanks!

This is awesome!! I’m new to scripting and powershell. Is there a way to add the logged on user to the script?

Awesome script for getting inventory on servers, however it can seem to get information on all our windows 7 clients? any ideas on this? ( i know our clients are locked down pretty tightly, firwall maybe? )

This script is very useful. However, I am trying to populate which user is on which computer (by looking at the last user to log in).

How can I manipulate the script below to do the network and add it into the excel sheet?:

$data = @()

$NetLogs = Get-WmiObject Win32_NetworkLoginProfile

foreach ($NetLog in $NetLogs) {
if ($NetLog.LastLogon -match “(\d{14})”) {
$row = “” | Select Name,LogonTime
$row.Name = $NetLog.Name
$row.LogonTime=[datetime]::ParseExact($matches[0], “yyyyMMddHHmmss”, $null)
$data += $row
}
}

$data

Hmmm…I wonder if this could be sent to a csv file. For example, then you could simply imnport into Excel if you must run it from a server that cannot have MS Office installed.

This is an awesome script, we are going to try at our campus, but we need it to one more thing…

It needs to get the computers serial numbers…where and how would I enter that code in…or if you could email me a version with the code already added…that would be great!

script is good and working in my domain, but i want to know how we can wrtite script to work only one OU ratherthan complete domain.

How about the serial number? Can we add that as well?

Thanks for sharing.

I would suggest you check out the the following:

The Microsoft Assessment and Planning Toolkit.

I found it will do all things this script will do, and give some other information you might find useful, like can this machine be virtualized!

http://technet.microsoft.com/en-us/library/bb977556.aspx

 

Leave a Comment

« | Home | »