The PowerShell script discussed in this post catalogs your Active Directory (AD) environment, figures out all the systems that are online, and then queries each of those systems for some common information.

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.

One of the most common tasks many system administrators have to do is keep track of all the systems in their environment. With virtual machine (VM) sprawl being a thing now, and with multiple teams bringing up servers left and right, it's sometimes hard for a sysadmin just to keep track of what's out there.

If she doesn't have an accurate gauge of how many systems are online and the kinds of resources they are consuming, she cannot make good decisions. This is why it's important to come up with a way to stay on top of your environment. One way to do this is with PowerShell. Since PowerShell has built-in capabilities to reach out and gather all kinds of information about each system, it's the perfect product to build a tool to make this happen in your environment.

First of all, we need to find some data source where we can gather all the computer names. This can be Active Directory, a SQL database or even a CSV file. As long as the names are in some structured format, PowerShell can probably gather them somehow.

In our case, we're using Active Directory, so we'll first need to get the ActiveDirectory module. We can do this by downloading and installing Remote Server Administration Tools. After doing this, you should then have the ActiveDirectory module available to you in a PowerShell session. It's now time to get to coding.

To index computer names in Active Directory, we'll use the Get-AdComputer command and capture them all into a variable. Below we're finding all computers in AD. But with the Filter parameter and the Where-Object command, you can easily limit the list down in some ways

Now that we have all the computer names, we can then loop through all of them and test each one to ensure it's online and available. For now, we'll just ping each one, but in this step, you can also do things like test PowerShell remoting, ensure files shares are available, and so on.

We're now reporting on offline computers and have space to continue performing other things on all the available computers as well.

Next, we need to query information from these computers somehow. For this demo, I'd like to get a CSV report looking something like this:

When querying information from systems, WMI is always the best bet. However, you must figure out which class stores the information. In our case, the Win32_OperatingSystem, Win32_PhysicalMemory and Win32_Processor classes store the information. The easiest way to gather this information is via the Get-CimInstance command.

Above, I'm gathering up all the information necessary and then creating a single object containing only the information I'd like to see. This then returns only the properties I'm looking for. I can run this now and see a nicely structured output showing the computer name along with the associated information next to each.

Example output

Example output

The only thing left to do is to get this into a CSV file. With the Export-Csv command tacked on to the end of our command for each object returned, this is a cinch.

Example CSV

Example CSV

This creates our CSV file, which contains all the information we were looking for! This example will give you a good base to build upon. This is by no means everything you can do here. Use this as a foundational tool to customize to your own needs!

Win the monthly 4sysops member prize for IT pros

Share
5+

Users who have LIKED this post:

  • avatar
  • avatar

Related Posts

5 Comments
  1. Robert Pearman 6 months ago

    Some other useful attributes to consider adding,

    Which should give you the Make, Model and Serial Number.

    I have also used the method above to store this info in AD in the Computer Description Field.

    1+

  2. Kent 6 months ago

    What does the "**" do? I cannot find any documentation on it and am receiving an error "You must provide a value expression following the '*' operator."

    0

    • Author
      Adam Bertram 6 months ago

      That looks like a formatting issue. I've updated the post.

      1+

  3. Matthew Davidson 6 months ago

    Great Article!  I also use the Win32_NetworkAdapterConfiguration in some scripts to grab the IP and MAC.  The possibilities are endless!!

    1+

    Users who have LIKED this comment:

    • avatar
  4. Mathieu 3 months ago

    if $computer is a physical one, maybe it could have more than one memory bank

    so for the psobject, it should be something like that :

    0

Leave a reply

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

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account