Yesterday I was at a customer who uses WDS, MDT in combination with RES Automation Manager to roll out Windows 7 workstations. We created a database in MDT to store the serial number, the mac address and the host name of a machine. This is used when deploying a workstation so the sys admins don’t have to fill in the computer name during deployment. One of the sys admins asked me if we could automate the action of putting the data in the MDT database so when a new machine is deployed it registers in the database without interaction.
So I wrote the following Powershell script which uses an additional powershell module named MDTDB.zip from this blog post by Michael Niehaus, you can download it by using this direct link. Place this on a share that can be reached by all workstations because the powershell script uses this module. To place a machine in the database for this customer we need three values: Mac address, hostname and serial number.
To get the mac adres:
# Get the values for the local host…
$strComputer = “.”#Get MacAdres and set $MC
$colItems = get-wmiobject -class “Win32_NetworkAdapterConfiguration” -namespace “root\CIMV2” -computername $strComputerforeach ($objItem in $colItems) {
# A test is needed here as the loop will find a number of virtual network configurations with no “Hostname”
# So if the “Hostname” does not exist, do NOT display it!
if ($objItem.DNSHostName -ne $NULL) {
$MC = $objItem.MACAddress
}
}
Now we’ve set the mac address to $MC we can focus on the serialnumber:
#Get SerialNumber
$SN = wmic bios get serialnumber| select-string -notmatch SerialNumber
So with wmic bios get serialnumber we get more output than we need so we select the string and exclude the text SerialNumber. This way the only value in $SN is the serialnumber. The only value we need now is the hostname, that one is pretty easy:
#Get Hostname
$HN = Hostname
Now we’ve got all the information to fill the database with this new machine, so we have to import the module MDTDB and connect to the MDT database:
#Import Modules
Import-Module –name “\\Servername\Sharename\MDTDB.psm1”#Connect to database
Connect-MDTDatabase -sqlserver SQLSERVERNAME -database DATABASENAME
With this connection made we can put the data into the database using the following script:
#Create new computer with specified data
New-MDTComputer –macAddress $MC -serialnumber $SN –settings @{OSInstall=’YES’;
OSDComputerName=$HN;
OrgName=’ORGANISATIONNAME’}
After a refresh of the MDT database you will see there’s a new record in the database. Now we can use this script to create a module in RES Automation Manager, just create a new module with a Powershell task. Selec the “Use Windows Powershell script from “Script’ tab” and copy/paste the script into the script tab:
Now you’re set to use this script during deployment. I’ve added it to the project that runs when the machine is added to a team a created so when a machine is deployed with WDS, MDT the RES Automation Agent is installed and based on the team it will run a certain project and this module is now part of this project.
You can download the script here.
Kees Baggerman
Latest posts by Kees Baggerman (see all)
- Nutanix AHV and Citrix MCS: Adding a persistent disk via Powershell – v2 - November 19, 2019
- Recovering a Protection Domain snapshot to a VM - September 13, 2019
- Checking power settings on VMs using powershell - September 11, 2019
- Updated: VM Reporting Script for Nutanix with Powershell - July 3, 2019
- Updated (again!): VM Reporting Script for Nutanix AHV/vSphere with Powershell - June 17, 2019
Some points on this post:
1. Rather than using WMIC to get the bios serial number, why not use WMI:
$SN = (Get-WmiObject Win32_Bios).SerialNumber
2. The approach to getting just the adapter configurations would be better expressed as:
$mc = (get-wmibobject win32_networkadapterconfiguration |
where {$_.DnsHostName})[0]
Thomas, thanks for the tips. Scripting is not day to day work for me so every improvement is welcome! I will test your comments and if they work I will see if I can change my post.
[…] good usecase would be the VM prep for an automated deployment with the Microsoft Deployment Toolkit for […]