Querying the MDT Database with PowerShell – by Joseph Moody

This post was contributed by Joseph Moody from http://deployhappiness.com

——-

When I first discovered the MDT database, I was immediately impressed by the power. Instead of having to support multiple task sequences or not skipping certain items in my CustomSettings.ini, I could let the MDT database dynamically assign settings.

As my organization relied on the database more, I had to stop editing devices by hand. Instead, I needed to learn how to use PowerShell. After some trial and error, I started getting the hang of it. For me, the most difficult part was learning how to query the database. Here is what you need to know to get started:

Downloading the PowerShell Module and Connecting to the Database

If you don't already have it, you'll need the MDTDB PowerShell module. The file can be found here. Extract the module and launch a PowerShell ISE console. Next, you will need to import the module. To import the module, type:

Import-Module -name "PATH TO MODULE\MDTDB.psm1"

If you will work with the MDT database a lot, you might want to add the command above to your PowerShell profile. To connect to your MDT database, type:

Connect-MDTDatabase -sqlServer SQLSERVERNAME.DOMAIN.LOCAL -database MDT

Exploring the Module (and your Database)

Using either the Script Explorer or the Get-Command cmdlet, find all of the Get-MDT* commands. As you can see in the screenshot below, PowerShell allows you to query your MDT database in pretty much any manner you could imagine.

001

The most common command that I use is "Get-MDTComputer". If you can master the Get-MDTComputer command, you can then pipe output to editing commands (Set-MDTComputer, Remove-MDTComputer). This will let you do massive changes in just seconds!

Get-MDTComputer: Example 1

With Get-MDTComputer, you can query your database using 5 factors:

  • assetTag
  • id
  • macAddress
  • serialNumber
  • uuid

To query by the database id, I could type Get-MDTComputer -ID IDNUMBER

002

If you've looked at the help file for Get-MDTComputer, you probably noticed that I left one possible query out, the -description switch.

You probably are thinking, "This is awesome! I already store the computer name in the description attribute. Searching will be so easy!" And searching by the Description would be easy if it worked. That is right, the -Description switch is broken!

003

Get-MDTComputer: Example 2

But don't fear, I have a solution for you! Simply pick another searching attribute that you do not use. In my environment, we didn't use the assettag attribute. When you import computers into your MDT database, write your description value to the assettag attribute at the same time.

Once you've done this, you can search the MDTDatabase for computer names. For example,

Get-MDTComputer -assetTag COMPUTERNAME

004

Wrapping Up

I've just covered how you can use PowerShell to query computers in your MDT database. Now that you've seen the potential for PowerShell + MDT, what do you think?

If you want to learn more about the MDT database (plus how to automatically import machines), head to DeployHappiness.com and subscribe by email. The new MDT Database management series will be starting soon.

About the author

Johan Arwidmark

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Marimar
Marimar
9 years ago

Great post, thanks!

To bad the -Description switch does not work.
However, if you want to search for a specific computername you could also run something like this to return the computerobject you're looking for:

$computer = get-mdtcomputer | Where-Object {$_.osdcomputername -eq "secc2973"}


>