Michael Niehaus' Windows and Office deployment ramblings
A couple of weeks ago at the Microsoft Management Summit conference in Las Vegas, I demonstrated some PowerShell scripts for doing a variety of things. I had promised to post those to my blog so that you could use them as well. This is the first of those scripts, designed to help maintain the contents of the MDT database. I have done some additional work on this one since I demonstrated it, and it’s now ready for your enjoyment.
This PowerShell script leverages the PowerShell 2.0 “advanced function” capabilities to write PowerShell cmdlets using PowerShell scripts – no compiled code is required. That does mean that you must be running PowerShell 2.0 CTP3 or later (e.g. the version of PowerShell included in Windows 7). There is no dependency on a particular version of MDT, so you can use this with MDT 2008, MDT 2010, or even BDD 2007 if you so choose.
The script files themselves are rather bare – lots of scripts, very few comments, and no documentation at all beyond what is in this blog post. There just aren’t enough hours in the day to do those types of things. It’s bad enough that the PowerShell module script has already grown to over 1,500 lines.
Here’s the quick list of Cmdlets included in the MDTDB.psm1 script and a very brief description of each:
Yes, there really are 69 different cmdlets in the script. I could have tried generalizing some of these, but then you’d have to provide more parameters to each one, so this actually simplifies things a little. A few notes:
So how do you use all of these? See the included “MDTDB_Test.ps1” script for some simple examples (used to test the logic). Here are a few more examples, just to give you some ideas.
Create a new computer and set its details
To add a new computer, just specify some identifying details (at least a MAC address, serial number, asset tag, or UUID, or any combination of those), along with whatever settings you want to configure. This example adds a machine, specifies its settings, and configures it with a list of applications:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase new-mdtcomputer -macAddress '00:00:00:11:22:33' -settings @{OSInstall='YES'; OSDComputerName='MYPC'} get-mdtcomputer -macaddress '00:00:00:11:22:33' | Set-MDTComputerApplication -applications @('{dc83b044-f018-40ec-828f-5e2b77058bb4}','{e7d31704-af9d-4105-ad4b-dbb95e23b692}')
You could actually shorten this too, since New-MDTComputer returns the new item which can be piped into Set-MDTComputerApplication:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase new-mdtcomputer -macAddress '00:00:00:11:22:33' -settings @{OSInstall='YES'; OSDComputerName='MYPC'} | Set-MDTComputerApplication -applications @('{dc83b044-f018-40ec-828f-5e2b77058bb4}','{e7d31704-af9d-4105-ad4b-dbb95e23b692}')
Import a list of makes and models
Assume you have a “Models.csv” file that looks like this (maybe exported from ConfigMgr):
Make,Model Dell,Latitude e6400 Lenovo,T61p HP,dx5150 Toshiba,M400
With that, you can then issue a simple PowerShell command to quickly create multiple MDT database entries:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase import-csv .\Models.csv | New-MDTMakeModel -settings @{OSInstall='YES'}
Create a new role and set its details
Creating a new role just requires specifying its name, along with any settings you want. In this case, I’ll also add a list of ConfigMgr packages that should be installed with this role:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase New-MDTRole -name 'My Role' -settings @{Home_Page = 'http://www.live.com'} | Set-MDTRolePackage -packages @('XXX00001:Install','XXX00002:Install')
List all contents
The “get” cmdlets can be used to retrieve a list of existing items by not specifying any criteria:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase Get-MDTComputer Get-MDTLocation Get-MDTMakeModel Get-MDTRole
Note that you might want to specify -detail on the Get-MDTLocation cmdlet to get the details; this will cause it to return one row per gateway value.
Add a package mapping entry
MDT provides a sample table and stored procedure for reinstalling software on a computer based on ConfigMgr inventory. To support this, you need to populate the package mapping table with entries to indicate which package should be installed for machine with a particular Add/Remove Programs entry. Adding an entry can be done fairly easily:
import-module .\MDTDB.psm1 connect-mdtdatabase -sqlserver MNiehaus-T61P-7 -instance SQLEXPRESS -database MDTDatabase New-MDTPackageMapping -ARPName 'Zune' -package 'XXX00003:Install'
PingBack from http://asp-net-hosting.simplynetdev.com/scripts-powershell-pour-gerer-la-base-de-donnees-mdt-2010/
Great blog entry Mike!
Thanks for this useful contribution.
http://blogs.technet.com/mniehaus/archive/2009/05/15/manipulating-the-microsoft-deployment-toolkit-database
Wow this is going to save a lot of time for setting up redundant systems. Thanks Mike!
You mention: "in MDT 2010 Beta 2 or later, where you can just point to the deployment share that contains all the needed details."
Would that be:
Connect-MDTDatabase -drivepath \\mdtserver\mdtpath
?
14. Set-MDTComputerRole doesn't seem to do exactly what it claims to, it deletes all existing roles and adds the selected one instead of adding it to the list of roles.
Bug?
On the Connect-MDTDatabase comment, you can do this by specifying the logical drive path. For example:
Connect-MDTDatabase -drivepath DS001:
Set-MDTComputerRole expects to be passed the whole list of roles to be assigned to a computer. So if you wanted to add one, you would need to use "Get-MDTComputerRole" to get the current list as an array, add a new item to that array, and then use Set-MDTComputerRole to set the list to the newly-extended version.
So, there's probably a better place to post this but. So far i LOVE this, thank you so much. but, I'm failing and coming up with a way to mass update machines in my database with the set-MDTComputer string. I failed to add a couple of settings when i mass imported my machines and am lacking in my powershell skills to be able to com up with the correct syntax to do such a thing. I'd also like to apply the same type of deal with set-MDTComputerApplication.
This looks very advanced to me.
I need simpler steps to install apps based on make and model of the computers.
is there any other way that is simpler compare to this?
thanks
Is there any way to add Description to the database? It's more friendly then MAC address.
Hello, and many many thanks. Any idea why Get-MDTComputer -description MYPCNAME always throws an error? Walking through the psm file, it should work, but the error is that ComputerSettings has no such column. In fact, in the output of Get-MDTComputer, no computer has the description listed. Is that intentional? Any help would be greatly appreciated. Thanks! --Matt
Sorry, I forgot to mention that for thousands of PCs, we use Description=OSDComputerName just so that I could support querying by name...
Hello, and thanks for this module
I just want ton know if it is possible to get just the ID of a location whith known name
something like
$ID = Get-MDTLocation.ID -name xxxx