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 https://blogs.technet.microsoft.com/mniehaus/2009/05/14/manipulating-the-microsoft-deployment-toolkit-database-using-powershell/
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: https://syscenramblings.wordpress.com/2016/01/15/mdt-database-the-powershell-module-fix/
The Script:
#Add Computer to the MDT Database 1.0 Param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidateLength(3,16)] [ValidatePattern('[A-Z][0-9]')] [String]$ComputerName, [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidateSet('Standard PC','RnD','Admin Workstation')] [String]$Role, [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidatePattern('^([0-9a-fA-F]{2}[:-]{0,1}){5}[0-9a-fA-F]{2}$')] [String]$MacAddress, [Switch]$Force ) #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{ Param( $ComputerName ) try { $Null = Get-ADComputer $ComputerName Return $True } Catch { Return $False } } #Create function for check MDT DB if name exists Function CheckIfComputerInMDTExists{ Param( $ComputerName ) $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{ Param( $MacAddress ) $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" if(!($Force)){BREAK} }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" if(!($Force)){BREAK} }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" if(!($Force)){BREAK} }else{Write-Host "MDT macaddress check OK"} #Create array for all settings the computer should have $Settings = @{ OSInstall='YES'; OSDComputerName="$ComputerName" } #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
/mike
Categories: MDT, OS Deployment, OSD, PowerShell