Friday, June 01, 2012

Powershell script to mass import computers into MDT database

So I'm setting up a new Windows Deployment Server with MDT and have decided to try out the database function on it. To get started all you have to do is install SQL express on the server and then while in the Microsoft Deployment Toolkit crate a new database and note down what its called.

Now if your running the same kind of network I am you've got hundreds of computers and adding them one by one into the database and then going into the details and further adding information into the hundred or so fields is just not realistic, so what can you do. Well this is a very nice script that I've knocked up and it will read the contents of a CSV file and import the settings into the database.

First things first. You need to download the modules and import them into powershell. You can download the modules from Microsoft from HERE. Once done create a folder called scripts or something and then extract the files to that folder. Now go into MDT and open up your database, click on 'Computers' and on the right choose 'new'. It doesn't matter if its real or not I just want you to see the details. Click on the details tab, and see all the options on the left hand side. These can all be headers in your CSV file. Now create your CSV file with the headers of Name, Mac, Assettag and continue with the headers from the details section when you add a new computer into the database. Once you fill in the CSV and saved it you can populate the database. Try it with just a couple of computers in the CSV file to start with. You can always delete them if it doesn't work.

First change the file extension on the CSV file to  .TXT. So in the below example i've changed mine to 'machines.txt'

Now copy the below script into Notepad and save it as a .ps1 file. This will let you open it with powershell.

You'll need to change the SERVERNAME to your servers name and DATABASENAME to what ever your called your database.

Import-Module –name C:\Scripts\MDTDB.psm1

Connect-MDTDatabase –sqlServer SERVERNAME –instance SQLEXPRESS –database DATABASENAME

$machines = Import-Csv C:\scripts\machines.txt

For ($i=1; $i -le $machines.count; $i++)

{

   New-MDTComputer -macAddress $machines[$i-1].mac -AssetTag $machines[$i-1].AssetTag -description $machines[$i-1].description -settings @{

    OSInstall='YES';
    OSDComputerName=$machines[$i-1].Name;
    OrgName='My ORG'
    DomainAdmin=$machines[$i-1].DomainAdmin;
    DomainAdminDomain=$machines[$i-1].DomainAdminDomain;
    DomainAdminPassword=$machines[$i-1].DomainAdminPassword;
    JoinDomain=$machines[$i-1].JoinDomain;
    _SMSTSORGNAME=$machines[$i-1]._SMSTSORGNAME;
    AdminPassword=$machines[$i-1].AdminPassword;
    Home_page=$machines[$i-1].Home_Page;
    ProductKey=$machines[$i-1].ProductKey
    ServerA=$machines[$i-1].ServerA;
    TaskSequenceID=$machines[$i-1].TaskSequenceID;
    UserDomain=$machines[$i-1].UserDomain;
    UserID=$machines[$i-1].UserID;
    UserPassword=$machines[$i-1].UserPassword;
    KeyboardLocale=$machines[$i-1].KeyboardLocale;
    SystemLocale=$machines[$i-1].SystemLocale;
    UILanguage=$machines[$i-1].UILanguage;
    UserLocale=$machines[$i-1].UserLocale;
    SkipAdminPassword=$machines[$i-1].SkipAdminPassword;
    SkipApplications=$machines[$i-1].skipApplications;
    SkipBitLocker=$machines[$i-1].SkipBitLocker;
    SkipBitLockerDetails=$machines[$i-1].SkipBitLockerDeatils;
    SkipBuild=$machines[$i-1].SkipBuild;
    SkipCapture=$machines[$i-1].SkipCapture;
    SkipComputerBackup=$machines[$i-1].SkipComputerBackup;
    SkipComputerName=$machines[$i-1].SkipComputerName;
    SkipDeploymentType=$machines[$i-1].SkipDeploymentType;
    SkipDestinationDisk=$machines[$i-1].SkipDestinationDisk;
    SkipDomainMembership=$machines[$i-1].SkipDomainMembership;
    SkipFinalSummary=$machines[$i-1].SkipFinalSummary;
    SkipLocaleSelection=$machines[$i-1].SkipLocaleSelection;
    SkipPackageDisplay=$machines[$i-1].SkipPackageDisplay;
    SkipProductKey=$machines[$i-1].SkipProductKey;
    SkipSummary=$machines[$i-1].SkipSummary;
    SkipTaskSequence=$machines[$i-1].SkipTaskSequence;
    SkipTimeZone=$machines[$i-1].SkipTimeZone;
    SkipUserData=$machines[$i-1].SkipUserData;
    SkipWizard=$machines[$i-1].SkipWizard;
   

   }

}
You can customize this script all you want. Uts very easy. The main section simply lists all the headers i've chosen to import into the database. Make sure they are all in your CSV/TXT file or you'll have a load of missing information