My Donet Web Mail Contact Us Arrow

Blog

Dynamic Documentation with Windows Powershell

Posted on by Eric Wrightcomment bubble0 Comments

 

Since my last post on the ‘Usefulness of Windows Powershell’ I have continued to use it on a daily basis and I find it more useful than ever. If you are in the realm of the Microsoft world and haven’t begun playing around with it I strongly suggest you do.  However, if you are a noob I suggest you do not run shells with administrator privileges until you get the hang of it.

Introduction
In the past server documentation has always tended to be static information placed in a central repository.  As businesses continue to grow the documentation updating process can tend to be tedious and easily forgotten.  This can be a headache for everyone involved, especially administrators looking for valid information.  Everyone wants to avoid extra line items in their budget and let’s face it, purchasing a document application is going to be pretty much last on the list; especially in this economy. 

Most documentation applications are coupled with different functionality such as:
1. Monitoring solutions and health services including Microsoft Systems Center Operations Manager (SCOM)
2. OS and Software deployment applications including Microsoft Systems Center Configuration Manager (SCCM)

These items tend to go above and beyond documenting your servers and tend to not give you all the information you are requiring.  If you are interested in learning more about SCOM or SCCM please feel free to give us a call as we are Microsoft Partners and SPLA providers.

Dynamic Documentation

With Windows Powershell you can easily obtain all the information you wish about a server on a daily basis using a simple script and Task Scheduler.  Once you get comfortable with the information you want to query you can move forward with querying all systems from a central location using WinRM.  We can obtain all information via WMI and regular command line queries to grab everything we care about as administrators.  With Server 2008 R2 everything you can think of is available via a WMI call.  For those of you who are noobs to WMI check ‘this site’ out as it is a great introduction provided by Microsoft.

WMI Classes for documentation

You may notice I use some shortcuts in the commands;  gwmi = get-wmiobject, gc = get-content. 

$ComputerSystem = gwmi win32_ComputerSystem
$SystemEnclosure = gwmi win32_SystemEnclosure
$Processor = gwmi win32_processor
$OperatingSystem = gwmi win32_OperatingSystem
$PageFileUsage = gwmi win32_PageFileUsage
$product = gwmi win32_product
$NetworkAdapter = gwmi win32_NetworkAdapter
$NetworkAdapterConfiguration = gwmi win32_NetworkAdapterConfiguration
$logicaldisk = gwmi win32_logicaldisk
$diskpartition = gwmi win32_diskpartition
$volume = gwmi win32_volume
$group = gwmi win32_group
$groupuser = gwmi win32_groupuser
$useraccount = gwmi win32_useraccount
$hostfile = gc c:\windows\system32\drivers\etc\hosts
$scheduledtasks = schtasks.exe /query /v /fo csv > c:\windows\temp\temp.csv;
$scheduledtasks = import-csv c:\windows\temp\temp.csv;
$firewall =(new-object -com HNetCfg.FwMgr).LocalPolicy.CurrentProfile

All the information that you care about the computer is now in memory.  For example if you issued the following command:
$computersystem.caption

Powershell would retrieve the computer name for you.  If you ever have an object in powershell and you don’t know all of it’s members (such as “.caption” in the above example) you can issue the following command:
$variable | get-member -membertype property

Play around with that for a while and you will love what you see.

Modifying data

Much of the data you get back in the above WMI calls is not relevant. You will need to ‘weed out’ items by issuing commands such as ‘where-object {$_. -eq “STRING”}’ or ‘where-object {$_. -notmatch “STRING”}’.  In order to make things look unified you will need to format items by splitting, removing, dividing, subtracting, etc.  Since this information is in memory we can modify these objects directly.
$computersystem.caption = “Hello”

However, I took an alternative approach by creating my own object with the modified data.
$serverinformation = new-object system.object
$serverinformation = Add-Member -type NoteProperty -name “Server_Name” -value $computersystem.caption

Outputting data

Once you have all of your information in an object it is very easy to output that object to a CSV file.  Simply pipe your object to an export-csv “filename” command you are finished!

$serverinformation | export-csv c:\serverinformation.csv

More advanced solution
The above solution may work for those companies with limited amount of servers.  However, if you see yourself managing 10, 20, 30 servers or more it is an unrealistic solution.  If you are looking for dynamic documentation solutions for those enterprise settings; research WinRM and look into storing your information in a database backend.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 
quote

David Mezera

President and Founder

A company is only as good as the people who work there. And in Donet's case, those people are simply exceptional. Bright. Inventive. Rock-solid.quote end

quote

Raleigh Sandy III

Vice President, Founder

You can expect that when you have
a problem, we're going to respond
to it. Quickly. Personally. And effectively.quote end

Social Header