The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

OS Deployment – Using the PowerShell to work with the MDT Database module: Sample 1

Posted by Mikael Nystrom on April 22, 2016

During the OSD class in Phoenix this week we worked with the MDT Database and some one asked if it was possible to use PowerShell to modify the database and and the same time verify if the mac address or the computer name was already in use before creating the database entry. The short answer was –Yes, of course. So I decided to create a sample on how that could look like.

Working with the database is pretty simple using the PowerShell module that Michael Niehaus created

So, using that module and the Active Directory PowerShell module means that we can now check if the Mac Address is already in use or if the computer name already exists in the MDT database or in Active Directory. The PowerShell script sample is using regex to verify that Mac Address as well as the computer name. Besides creating the object if it does not exist (or you can use the –Force switch to override) it also adds a database role to the computer.

Note: That you should also fix the database, since it is broken by default, just follow these steps:

The Script:

#Add Computer to the MDT Database 1.0

    [ValidateSet('Standard PC','RnD','Admin Workstation')] 



#Import the Modules and connect to the database
Import-Module MDTDB -ErrorAction Stop
Import-Module ActiveDirectory -ErrorAction Stop
Connect-MDTDatabase -sqlServer MDT01 -database MDT01 -instance SQLExpress -ErrorAction stop

#Create function for check AD if name exists
Function CheckIfComputerInADExists{
    try {
        $Null = Get-ADComputer $ComputerName
        Return $True
    Catch {
        Return $False

#Create function for check MDT DB if name exists
Function CheckIfComputerInMDTExists{
    $result = Get-MDTComputer | Where-Object -Property OSDComputerName -EQ -Value $ComputerName
    if($result -ne $null){Return $True}else{$False}

#Create function for check MDT DB if MAC exists
Function CheckIfMacAddressInMDTExists{
    $result = Get-MDTComputer -macAddress $MacAddress
    if($result -ne $null){Return $True}else{$False}

#Check if Computer exists in Active Directory
$CheckAD = CheckIfComputerInADExists -ComputerName $ComputerName
if($CheckAD -eq $true){
    Write-Warning "$ComputerName exists in Active Directory"
    }else{Write-Host "AD Name check OK"}

#Check if Computer exists in the MDT database
$CheckMDT = CheckIfComputerInMDTExists -ComputerName $ComputerName
if($CheckMDT -eq $true){
    Write-Warning "$ComputerName exists in the MDT database"
    }else{Write-Host "MDT name check OK"}

#Check if MacAddress exists in the MDT database
$CheckMAC = CheckIfMacAddressInMDTExists -MacAddress $MacAddress
if($CheckMAC -eq $true){
    Write-Warning "$MacAddress exists in the MDT database"
    }else{Write-Host "MDT macaddress check OK"}

#Create array for all settings the computer should have
$Settings = @{

#If computer name exists and we used the -Force switch, remove it
if($CheckMDT -eq $true){
    Get-MDTComputer -description $ComputerName | Remove-MDTComputer

#If MacAddress exists and we used the -Force switch, remove it
if($CheckMAC -eq $true){
    Get-MDTComputer -macAddress $MacAddress | Remove-MDTComputer

#Create Computer in MDT Database
$NewMDTComputer = New-MDTComputer -macAddress $MacAddress -description $ComputerName -settings $Settings

#Add role to Computer in MDT Database
$NewMDTComputer | Set-MDTComputerRole -roles $Role


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: